@goodtiming/baton-sdk 0.5.7 → 0.6.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 +15 -4
- package/dist/index.cjs +332 -181
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +289 -23
- package/dist/index.d.ts +289 -23
- package/dist/index.js +331 -181
- 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.
|
|
2
|
+
var SDK_VERSION = "ts-0.6.1";
|
|
3
3
|
|
|
4
4
|
// src/events.ts
|
|
5
5
|
import { z } from "zod";
|
|
@@ -144,6 +144,7 @@ var envelopeShape = {
|
|
|
144
144
|
// consumer names the client from `client_observed`.
|
|
145
145
|
agent_runtime: z.string().default("unknown"),
|
|
146
146
|
principal: PrincipalWireSchema.nullable().default(null),
|
|
147
|
+
organization: PrincipalWireSchema.nullable().default(null),
|
|
147
148
|
transport_observed: z.string().nullable().default(null),
|
|
148
149
|
session_id_origin: z.string().nullable().default(null),
|
|
149
150
|
client_observed: ClientObservedSchema.nullable().default(null),
|
|
@@ -735,59 +736,113 @@ var LONE_SURROGATE = /\p{Surrogate}/u;
|
|
|
735
736
|
function pythonStrip(value) {
|
|
736
737
|
return value.replace(PYTHON_STRIP, "");
|
|
737
738
|
}
|
|
738
|
-
function
|
|
739
|
+
function usableId(id) {
|
|
740
|
+
if (typeof id !== "string") return false;
|
|
741
|
+
if (LONE_SURROGATE.test(id)) return false;
|
|
742
|
+
if (id.includes("\0")) return false;
|
|
743
|
+
return pythonStrip(id) !== "";
|
|
744
|
+
}
|
|
745
|
+
function readIdentity(result, idKey) {
|
|
739
746
|
if (result === null || typeof result !== "object") return null;
|
|
740
747
|
const candidate = result;
|
|
741
|
-
|
|
742
|
-
if (
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
748
|
+
const id = candidate[idKey];
|
|
749
|
+
if (!usableId(id)) return null;
|
|
750
|
+
const displayName = typeof candidate.displayName === "string" ? candidate.displayName : void 0;
|
|
751
|
+
return { id, form: candidate.form, displayName };
|
|
752
|
+
}
|
|
753
|
+
var NO_CALLER = Object.freeze({ principal: null, organization: null });
|
|
754
|
+
function callerFor(result, warn4) {
|
|
755
|
+
try {
|
|
756
|
+
return readCaller(result, warn4);
|
|
757
|
+
} catch {
|
|
758
|
+
warn4("unreadable", "could not be read \u2014 sending neither principal nor organization.");
|
|
759
|
+
return NO_CALLER;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
function readCaller(result, warn4) {
|
|
763
|
+
if (result === void 0 || result === null) return NO_CALLER;
|
|
764
|
+
if (typeof result !== "object") {
|
|
765
|
+
warn4("not-object", `got a ${typeof result}, not an object \u2014 sending neither principal nor organization.`);
|
|
766
|
+
return NO_CALLER;
|
|
767
|
+
}
|
|
768
|
+
if (!("principalId" in result) && !("organization" in result)) {
|
|
769
|
+
if ("organizationId" in result) {
|
|
770
|
+
return { principal: null, organization: identityFor(result, "organization", warn4) };
|
|
771
|
+
}
|
|
772
|
+
if (typeof result.userId === "string") {
|
|
773
|
+
warn4(
|
|
774
|
+
"user-id",
|
|
775
|
+
"returned { userId }, which was renamed to { principalId } in 0.3.5, so the principal is dropped from every event until it returns the new key."
|
|
776
|
+
);
|
|
777
|
+
} else {
|
|
778
|
+
warn4("no-id", "got an object with neither principalId nor organizationId \u2014 sending neither.");
|
|
779
|
+
}
|
|
780
|
+
return NO_CALLER;
|
|
781
|
+
}
|
|
782
|
+
if ("organizationId" in result) {
|
|
783
|
+
warn4("flat-organization-id", "got principalId and organizationId side by side; put the company in `organization`.");
|
|
784
|
+
}
|
|
785
|
+
const nested = result.organization;
|
|
786
|
+
return {
|
|
787
|
+
principal: identityFor(result, "principal", warn4),
|
|
788
|
+
organization: nested == null ? null : identityFor(nested, "organization", warn4)
|
|
789
|
+
};
|
|
790
|
+
}
|
|
791
|
+
function identityFor(value, member, warn4) {
|
|
792
|
+
const idKey = member === "principal" ? "principalId" : "organizationId";
|
|
793
|
+
const identity = readIdentity(value, idKey);
|
|
794
|
+
if (identity === null) {
|
|
795
|
+
warn4(`${member}:id`, `${member} has no usable ${idKey} \u2014 sending no ${member}.`);
|
|
796
|
+
return null;
|
|
797
|
+
}
|
|
798
|
+
return identityWire(
|
|
799
|
+
identity.id,
|
|
800
|
+
identity.form,
|
|
801
|
+
identity.displayName,
|
|
802
|
+
(form) => warn4(`${member}:form`, `${member} form ${describeForm(form)} is not registered \u2014 sending "raw".`)
|
|
803
|
+
);
|
|
804
|
+
}
|
|
805
|
+
function describeForm(form) {
|
|
806
|
+
return typeof form === "string" ? JSON.stringify(form.slice(0, 32)) : `of type ${typeof form}`;
|
|
807
|
+
}
|
|
808
|
+
function identityWire(id, stated, displayName, onUnregisteredForm) {
|
|
751
809
|
let form = PRINCIPAL_FORM_RAW;
|
|
752
|
-
if (
|
|
753
|
-
if (PRINCIPAL_FORMS.has(
|
|
754
|
-
form =
|
|
810
|
+
if (stated !== void 0) {
|
|
811
|
+
if (PRINCIPAL_FORMS.has(stated)) {
|
|
812
|
+
form = stated;
|
|
755
813
|
} else {
|
|
756
|
-
onUnregisteredForm?.(
|
|
814
|
+
onUnregisteredForm?.(stated);
|
|
757
815
|
}
|
|
758
816
|
}
|
|
759
817
|
const wire = {
|
|
760
818
|
// `capCodePoints`, not `.slice()`: a plain slice counts UTF-16 units, so
|
|
761
819
|
// an astral id cut at the boundary would ship a lone surrogate.
|
|
762
|
-
id: capCodePoints(
|
|
820
|
+
id: capCodePoints(id, PRINCIPAL_ID_MAX_LEN),
|
|
763
821
|
source: PRINCIPAL_SOURCE_ASSERTED,
|
|
764
822
|
form
|
|
765
823
|
};
|
|
766
|
-
const
|
|
767
|
-
if (
|
|
824
|
+
const name = wireDisplayName(displayName);
|
|
825
|
+
if (name !== null) wire.display_name = name;
|
|
768
826
|
return wire;
|
|
769
827
|
}
|
|
770
828
|
|
|
771
829
|
// src/integrations/mcp/llmText.ts
|
|
772
|
-
var
|
|
773
|
-
|
|
774
|
-
|
|
830
|
+
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.`;
|
|
831
|
+
var USER_GOAL_FORMAT = "'<n>: <what this call is for>; success is <what its result should show>'";
|
|
832
|
+
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.
|
|
775
833
|
|
|
776
|
-
|
|
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.
|
|
834
|
+
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
835
|
|
|
779
836
|
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.
|
|
837
|
+
- user_goal: the same ${USER_GOAL_FORMAT} as on this server's other tools.
|
|
838
|
+
- 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.
|
|
839
|
+
- tool_name: REQUIRED. The ${vendorDisplayName} tool that went wrong, or none if no tool exists for the request.
|
|
840
|
+
- suggested_improvement: one concrete sentence on the product change that would have helped.
|
|
786
841
|
- 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
842
|
var CLAUDE_CODE_TRUNCATION_CAP = 2087;
|
|
788
843
|
var INSTRUCTIONS_LENGTH_CAP = 1500;
|
|
789
844
|
function renderServerInstructions(options) {
|
|
790
|
-
return
|
|
845
|
+
return REPORT_POINTER_TEMPLATE(options.vendorDisplayName, options.annotationToolName);
|
|
791
846
|
}
|
|
792
847
|
function fitsInstructionsCap(options) {
|
|
793
848
|
return renderServerInstructions(options).length <= INSTRUCTIONS_LENGTH_CAP;
|
|
@@ -799,7 +854,7 @@ function buildServerInstructions(options) {
|
|
|
799
854
|
`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
855
|
);
|
|
801
856
|
}
|
|
802
|
-
return
|
|
857
|
+
return rendered + INSTRUCTIONS_SUBAGENT_CLAUSE;
|
|
803
858
|
}
|
|
804
859
|
function buildAnnotationToolDescription(options) {
|
|
805
860
|
return ANNOTATION_TOOL_DESCRIPTION_TEMPLATE(options.vendorDisplayName);
|
|
@@ -807,39 +862,68 @@ function buildAnnotationToolDescription(options) {
|
|
|
807
862
|
var USER_GOAL_PARAM_NAME = "user_goal";
|
|
808
863
|
var EXPECTED_RESULT_PARAM_NAME = "expected_result";
|
|
809
864
|
var OVERALL_TASK_PARAM_NAME = "overall_task";
|
|
865
|
+
var RETIRED_PARAM_NAMES = [EXPECTED_RESULT_PARAM_NAME, OVERALL_TASK_PARAM_NAME];
|
|
810
866
|
var INTENT_SOURCE_PARAM = "injected_param";
|
|
811
867
|
var INTENT_SOURCE_LIBRARY = "library_arg";
|
|
812
|
-
var USER_GOAL_PARAM_BODY =
|
|
868
|
+
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
869
|
var USER_GOAL_PARAM_DESCRIPTION = "OPTIONAL. " + USER_GOAL_PARAM_BODY;
|
|
814
870
|
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.";
|
|
871
|
+
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.";
|
|
872
|
+
var INTENT_INSTRUCTIONS = INSTRUCTIONS_SUBAGENT_CLAUSE.trim();
|
|
822
873
|
function buildUserGoalParamDescription(options = {}) {
|
|
823
874
|
return options.intentParamMode === "required" ? USER_GOAL_PARAM_DESCRIPTION_REQUIRED : USER_GOAL_PARAM_DESCRIPTION;
|
|
824
875
|
}
|
|
825
|
-
function
|
|
826
|
-
return
|
|
876
|
+
function requiredParamNames(intentParamMode) {
|
|
877
|
+
return intentParamMode === "required" ? [USER_GOAL_PARAM_NAME] : [];
|
|
827
878
|
}
|
|
828
|
-
function
|
|
829
|
-
|
|
879
|
+
function declaresParam(schema, name) {
|
|
880
|
+
const props = schema?.properties;
|
|
881
|
+
return typeof props === "object" && props !== null && name in props;
|
|
830
882
|
}
|
|
831
|
-
function
|
|
832
|
-
|
|
883
|
+
function instructionsSuffixServed(annotationToolName, served) {
|
|
884
|
+
if (annotationToolName) return true;
|
|
885
|
+
return typeof served === "string" && served.includes(INTENT_INSTRUCTIONS);
|
|
833
886
|
}
|
|
834
887
|
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" })
|
|
888
|
+
Object.freeze({ name: USER_GOAL_PARAM_NAME, description: USER_GOAL_PARAM_BODY, traceOption: "intent" })
|
|
842
889
|
]);
|
|
890
|
+
var VENDOR_INSTRUCTIONS = /* @__PURE__ */ Symbol("batonVendorInstructions");
|
|
891
|
+
var escapeRegExp = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
892
|
+
var OURS_STARTS = [
|
|
893
|
+
new RegExp(
|
|
894
|
+
"^" + REPORT_POINTER_TEMPLATE("\0", "\0").split("\0").map(escapeRegExp).join(".+?")
|
|
895
|
+
),
|
|
896
|
+
new RegExp("^" + escapeRegExp(INTENT_INSTRUCTIONS))
|
|
897
|
+
];
|
|
898
|
+
function endsInOurs(current2, vendor) {
|
|
899
|
+
if (typeof current2 !== "string") return false;
|
|
900
|
+
const prefix = typeof vendor === "string" && vendor.trim() ? `${vendor}
|
|
901
|
+
|
|
902
|
+
` : "";
|
|
903
|
+
if (!current2.startsWith(prefix)) return false;
|
|
904
|
+
const rest = current2.slice(prefix.length);
|
|
905
|
+
return OURS_STARTS.some((start) => start.test(rest));
|
|
906
|
+
}
|
|
907
|
+
function withVendorInstructions(server, ours, warn4 = (message) => process.stderr.write(message)) {
|
|
908
|
+
const holder = server;
|
|
909
|
+
const current2 = holder._instructions;
|
|
910
|
+
let vendor = VENDOR_INSTRUCTIONS in holder ? holder[VENDOR_INSTRUCTIONS] : current2;
|
|
911
|
+
if (!endsInOurs(current2, vendor)) {
|
|
912
|
+
vendor = current2;
|
|
913
|
+
holder[VENDOR_INSTRUCTIONS] = vendor;
|
|
914
|
+
}
|
|
915
|
+
if (typeof vendor !== "string" || !vendor.trim()) return ours;
|
|
916
|
+
const combined = `${vendor}
|
|
917
|
+
|
|
918
|
+
${ours}`;
|
|
919
|
+
if (combined.length > CLAUDE_CODE_TRUNCATION_CAP) {
|
|
920
|
+
warn4(
|
|
921
|
+
`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
|
|
922
|
+
`
|
|
923
|
+
);
|
|
924
|
+
}
|
|
925
|
+
return combined;
|
|
926
|
+
}
|
|
843
927
|
|
|
844
928
|
// src/integrations/mcp/annotationName.ts
|
|
845
929
|
var TOOL_NAME_PATTERN = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
@@ -1014,47 +1098,32 @@ async function withinHookBudget(answer) {
|
|
|
1014
1098
|
clearTimeout(timer);
|
|
1015
1099
|
}
|
|
1016
1100
|
}
|
|
1017
|
-
var
|
|
1018
|
-
function
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
warn(
|
|
1023
|
-
"baton: resolvePrincipal returned { userId }, which was renamed to { principalId } in 0.3.5, so the principal is dropped from every event until the hook returns the new key."
|
|
1024
|
-
);
|
|
1101
|
+
var warned = /* @__PURE__ */ new Set();
|
|
1102
|
+
function warnOnce(key, message) {
|
|
1103
|
+
if (warned.has(key)) return;
|
|
1104
|
+
warned.add(key);
|
|
1105
|
+
warn(`baton: resolvePrincipal ${message}`);
|
|
1025
1106
|
}
|
|
1026
|
-
async function
|
|
1027
|
-
if (hook === void 0) return
|
|
1107
|
+
async function resolveCaller(hook, call) {
|
|
1108
|
+
if (hook === void 0) return NO_CALLER;
|
|
1028
1109
|
try {
|
|
1029
1110
|
const context = buildPrincipalResolutionContext(call.extra, call.toolName, call.arguments);
|
|
1030
1111
|
const result = await withinHookBudget(hook(context));
|
|
1031
|
-
|
|
1032
|
-
if (principal === null) {
|
|
1033
|
-
warnIfPreRenameShape(result);
|
|
1034
|
-
return null;
|
|
1035
|
-
}
|
|
1036
|
-
return principalFor(principal, (form) => {
|
|
1037
|
-
warn(
|
|
1038
|
-
`baton: resolvePrincipal returned form ${describeForm(form)}, expected "raw" or "hashed" \u2014 sending it as "${PRINCIPAL_FORM_RAW}".`
|
|
1039
|
-
);
|
|
1040
|
-
});
|
|
1112
|
+
return callerFor(result, warnOnce);
|
|
1041
1113
|
} catch {
|
|
1042
|
-
return
|
|
1114
|
+
return NO_CALLER;
|
|
1043
1115
|
}
|
|
1044
1116
|
}
|
|
1045
|
-
function describeForm(form) {
|
|
1046
|
-
return typeof form === "string" ? JSON.stringify(form.slice(0, 32)) : `of type ${typeof form}`;
|
|
1047
|
-
}
|
|
1048
1117
|
|
|
1049
1118
|
// src/integrations/mcp/safeScrub.ts
|
|
1050
|
-
var
|
|
1119
|
+
var warned2 = /* @__PURE__ */ new Set();
|
|
1051
1120
|
function scrubOrNull(scrubber, value, field) {
|
|
1052
1121
|
if (value === null) return null;
|
|
1053
1122
|
try {
|
|
1054
1123
|
return scrubber(value);
|
|
1055
1124
|
} catch (err) {
|
|
1056
|
-
if (!
|
|
1057
|
-
|
|
1125
|
+
if (!warned2.has(field)) {
|
|
1126
|
+
warned2.add(field);
|
|
1058
1127
|
process.stderr.write(
|
|
1059
1128
|
`baton: scrubber threw on ${field}; that field is dropped, tool call continues. Further failures on this field will be silent: ${String(err)}
|
|
1060
1129
|
`
|
|
@@ -1196,12 +1265,10 @@ function registerAnnotationTool(server, options) {
|
|
|
1196
1265
|
description,
|
|
1197
1266
|
inputSchema: {
|
|
1198
1267
|
user_goal: z2.string(),
|
|
1199
|
-
expected_result: z2.string().optional(),
|
|
1200
1268
|
what_happened: z2.string().optional(),
|
|
1201
1269
|
// Nullable so a report whose agent sends an explicit null is taken,
|
|
1202
1270
|
// not rejected by validation.
|
|
1203
1271
|
tool_name: z2.string().nullable().optional(),
|
|
1204
|
-
overall_task: z2.string().optional(),
|
|
1205
1272
|
suggested_improvement: z2.string().optional(),
|
|
1206
1273
|
context: z2.record(z2.string(), z2.unknown()).optional()
|
|
1207
1274
|
}
|
|
@@ -1229,7 +1296,7 @@ function registerAnnotationTool(server, options) {
|
|
|
1229
1296
|
"_meta"
|
|
1230
1297
|
);
|
|
1231
1298
|
const { sessionId, origin } = await resolveSessionId(options.fallbackSessionId, extra);
|
|
1232
|
-
const
|
|
1299
|
+
const caller = await resolveCaller(
|
|
1233
1300
|
options.resolvePrincipal,
|
|
1234
1301
|
{ extra, toolName: name, arguments: args }
|
|
1235
1302
|
);
|
|
@@ -1242,27 +1309,20 @@ function registerAnnotationTool(server, options) {
|
|
|
1242
1309
|
sequence_number: options.counter.next(sessionId),
|
|
1243
1310
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1244
1311
|
consent_token: options.consentToken,
|
|
1245
|
-
principal,
|
|
1312
|
+
principal: caller.principal,
|
|
1313
|
+
organization: caller.organization,
|
|
1246
1314
|
transport_observed: observeTransport(extra),
|
|
1247
1315
|
session_id_origin: origin,
|
|
1248
1316
|
client_observed: observeClient(extra, { server, scrubber: options.scrubber }),
|
|
1249
1317
|
runtime_meta: scrubbedMeta,
|
|
1250
1318
|
payload: {
|
|
1251
|
-
//
|
|
1252
|
-
// below: `user_goal` is stored as `intent`, `expected_result` as
|
|
1253
|
-
// `expected_outcome`.
|
|
1319
|
+
// The agent-facing `user_goal` is stored as the wire key `intent`.
|
|
1254
1320
|
// 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
1321
|
intent: options.scrubber(args.user_goal),
|
|
1256
|
-
expected_outcome: args.expected_result ? options.scrubber(args.expected_result) : null,
|
|
1257
1322
|
// 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
1323
|
what_happened: options.scrubber(whatHappened),
|
|
1259
1324
|
// 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
1325
|
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
1326
|
suggested_improvement: args.suggested_improvement ? options.scrubber(args.suggested_improvement) : null,
|
|
1267
1327
|
// 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
1328
|
context: args.context ? options.scrubber(args.context) : null
|
|
@@ -1482,11 +1542,14 @@ function sealKey(dsn) {
|
|
|
1482
1542
|
// src/integrations/mcp/errorResult.ts
|
|
1483
1543
|
var TOOL_ERROR_TYPE = "tool_error";
|
|
1484
1544
|
var OUTPUT_SCHEMA_MISMATCH = "output_schema_mismatch";
|
|
1545
|
+
var REFUSED = "refused";
|
|
1485
1546
|
var TASK_MODES = /* @__PURE__ */ new Set(["required", "optional"]);
|
|
1486
|
-
function requestSideFailureKind(entry) {
|
|
1547
|
+
function requestSideFailureKind(entry, validation) {
|
|
1487
1548
|
if (entry == null) return "unknown_tool";
|
|
1488
1549
|
if (entry.enabled === false) return "tool_disabled";
|
|
1489
|
-
|
|
1550
|
+
if (validation === "rejected") return "invalid_argument";
|
|
1551
|
+
if (TASK_MODES.has(entry.execution?.taskSupport)) return void 0;
|
|
1552
|
+
return validation === "unprobed" ? "invalid_argument" : REFUSED;
|
|
1490
1553
|
}
|
|
1491
1554
|
var ERROR_BODY_MAX_CODE_POINTS = 2e3;
|
|
1492
1555
|
function isErrorResult(value) {
|
|
@@ -1537,6 +1600,9 @@ function resultDerivedFields(mode, text, result, errorBody2, scrubber) {
|
|
|
1537
1600
|
function requestSideErrorFields(text, errorBody2) {
|
|
1538
1601
|
return { error_body: errorBody2(text), result: null };
|
|
1539
1602
|
}
|
|
1603
|
+
function requestSideFailureFields(kind, mode, text, result, errorBody2, scrubber) {
|
|
1604
|
+
return kind === REFUSED ? resultDerivedFields(mode, text, result ?? null, errorBody2, scrubber) : requestSideErrorFields(text(), errorBody2);
|
|
1605
|
+
}
|
|
1540
1606
|
function errorTypeOf(err) {
|
|
1541
1607
|
return err instanceof Error ? err.constructor.name : "Error";
|
|
1542
1608
|
}
|
|
@@ -1548,7 +1614,8 @@ function scrubbedErrorBody(scrubber, text) {
|
|
|
1548
1614
|
}
|
|
1549
1615
|
|
|
1550
1616
|
// src/integrations/mcp/config.ts
|
|
1551
|
-
var INTENT_PARAM_MODES = /* @__PURE__ */ new Set(["optional", "required"
|
|
1617
|
+
var INTENT_PARAM_MODES = /* @__PURE__ */ new Set(["optional", "required"]);
|
|
1618
|
+
var REPORT_MODES = /* @__PURE__ */ new Set(["on", "off"]);
|
|
1552
1619
|
var RESULT_CAPTURE_MODE_SET = new Set(RESULT_CAPTURE_MODES);
|
|
1553
1620
|
function resolveTenantId(explicit, vendorId2) {
|
|
1554
1621
|
if (explicit) return explicit;
|
|
@@ -1633,9 +1700,14 @@ function validateBatonConfig(config) {
|
|
|
1633
1700
|
`vendorId ${JSON.stringify(config.vendorId)} must match ${VENDOR_ID_PATTERN.source} \u2014 used as the default annotation tool name prefix.`
|
|
1634
1701
|
);
|
|
1635
1702
|
}
|
|
1636
|
-
if (!config.
|
|
1703
|
+
if (config.reportMode !== void 0 && !REPORT_MODES.has(config.reportMode)) {
|
|
1704
|
+
throw new Error(
|
|
1705
|
+
`BatonConfig.reportMode ${JSON.stringify(config.reportMode)} must be one of ${JSON.stringify([...REPORT_MODES].sort())}.`
|
|
1706
|
+
);
|
|
1707
|
+
}
|
|
1708
|
+
if (!config.vendorDisplayName && config.reportMode === "on") {
|
|
1637
1709
|
throw new Error(
|
|
1638
|
-
|
|
1710
|
+
'BatonConfig needs a vendorDisplayName with reportMode: "on" \u2014 used in server instructions and the annotation tool description (whitelabel obligation, SPEC \xA75.4).'
|
|
1639
1711
|
);
|
|
1640
1712
|
}
|
|
1641
1713
|
if (config.consentToken !== void 0 && !config.consentToken) {
|
|
@@ -1648,6 +1720,11 @@ function validateBatonConfig(config) {
|
|
|
1648
1720
|
"BatonConfig.consentToken is required per SPEC \xA72.3 \u2014 events without a valid consent_token MUST be rejected by the consumer."
|
|
1649
1721
|
);
|
|
1650
1722
|
}
|
|
1723
|
+
if (config.intentParamMode === "off") {
|
|
1724
|
+
throw new Error(
|
|
1725
|
+
'BatonConfig.intentParamMode: "off" would leave nothing capturing what the user is trying to do. Use "required" (the default) or "optional".'
|
|
1726
|
+
);
|
|
1727
|
+
}
|
|
1651
1728
|
if (config.intentParamMode !== void 0 && !INTENT_PARAM_MODES.has(config.intentParamMode)) {
|
|
1652
1729
|
throw new Error(
|
|
1653
1730
|
`BatonConfig.intentParamMode ${JSON.stringify(config.intentParamMode)} must be one of ${JSON.stringify([...INTENT_PARAM_MODES].sort())}.`
|
|
@@ -1833,17 +1910,14 @@ function injectGoalParams(inputSchema, intentParamMode) {
|
|
|
1833
1910
|
function buildIntentFields(existingShape, zodImpl, intentParamMode) {
|
|
1834
1911
|
const dispositions = {};
|
|
1835
1912
|
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
|
-
}
|
|
1913
|
+
for (const name of RETIRED_PARAM_NAMES) {
|
|
1914
|
+
if (name in existingShape) dispositions[name] = "native";
|
|
1915
|
+
}
|
|
1916
|
+
if (USER_GOAL_PARAM_NAME in existingShape) {
|
|
1917
|
+
dispositions[USER_GOAL_PARAM_NAME] = "native";
|
|
1918
|
+
} else {
|
|
1919
|
+
dispositions[USER_GOAL_PARAM_NAME] = "injected";
|
|
1920
|
+
toInject[USER_GOAL_PARAM_NAME] = zodImpl.string().optional().describe(buildUserGoalParamDescription({ intentParamMode }));
|
|
1847
1921
|
}
|
|
1848
1922
|
return { dispositions, toInject };
|
|
1849
1923
|
}
|
|
@@ -1923,12 +1997,12 @@ function assembleSurface(serverMeta, tools) {
|
|
|
1923
1997
|
function buildSeamAugmentations(options) {
|
|
1924
1998
|
return {
|
|
1925
1999
|
injected_tools: [...options.injectedToolNames].sort(),
|
|
1926
|
-
intent_param:
|
|
2000
|
+
intent_param: {
|
|
1927
2001
|
names: [...options.intentParamNames].sort(),
|
|
1928
2002
|
required_names: [...options.requiredParamNames].sort(),
|
|
1929
2003
|
mode: options.intentParamMode
|
|
1930
|
-
}
|
|
1931
|
-
instructions_suffix:
|
|
2004
|
+
},
|
|
2005
|
+
instructions_suffix: options.instructionsSuffix
|
|
1932
2006
|
};
|
|
1933
2007
|
}
|
|
1934
2008
|
|
|
@@ -1998,23 +2072,16 @@ async function resolveEnvelopeParts(ctx, extra) {
|
|
|
1998
2072
|
const session = await resolveSessionId(ctx.fallbackSessionId, extra);
|
|
1999
2073
|
return { ...session, scrubbedMeta };
|
|
2000
2074
|
}
|
|
2001
|
-
async function openCall(ctx, toolName, extra, params, registered = true) {
|
|
2075
|
+
async function openCall(ctx, toolName, extra, params, retired, registered = true) {
|
|
2002
2076
|
await maybeEmitSurfaceSnapshot(ctx);
|
|
2003
2077
|
const { sessionId, origin, scrubbedMeta } = await resolveEnvelopeParts(ctx, extra);
|
|
2004
2078
|
const dispositions = ctx.paramRegistry.get(toolName);
|
|
2005
2079
|
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
2080
|
const scrubbedIntent = scrubOrNull(ctx.scrubber, rawIntent, USER_GOAL_PARAM_NAME);
|
|
2015
|
-
const scrubbedExpected =
|
|
2016
|
-
|
|
2017
|
-
|
|
2081
|
+
const [scrubbedExpected = null, scrubbedWorkflow = null] = RETIRED_PARAM_NAMES.map(
|
|
2082
|
+
(name) => dispositions === void 0 || dispositions[name] === "native" ? null : scrubOrNull(ctx.scrubber, retired[name] ?? popText(params, name), name)
|
|
2083
|
+
);
|
|
2084
|
+
const caller = await resolveCaller(
|
|
2018
2085
|
ctx.resolvePrincipal,
|
|
2019
2086
|
{ extra, toolName, arguments: params }
|
|
2020
2087
|
);
|
|
@@ -2027,7 +2094,8 @@ async function openCall(ctx, toolName, extra, params, registered = true) {
|
|
|
2027
2094
|
// it. `surface_snapshot` is deliberately not among them: it describes the
|
|
2028
2095
|
// SERVER and is captured outside any call, so it has no caller, transport
|
|
2029
2096
|
// or client to name.
|
|
2030
|
-
principal,
|
|
2097
|
+
principal: caller.principal,
|
|
2098
|
+
organization: caller.organization,
|
|
2031
2099
|
transport_observed: observeTransport(extra),
|
|
2032
2100
|
session_id_origin: origin,
|
|
2033
2101
|
client_observed: observeClient(extra, ctx),
|
|
@@ -2060,7 +2128,9 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
2060
2128
|
if (slot) slot.innerFired = true;
|
|
2061
2129
|
const extra = callArgs[callArgs.length - 1];
|
|
2062
2130
|
const params = (callArgs.length > 1 ? callArgs[0] : void 0) ?? {};
|
|
2063
|
-
const
|
|
2131
|
+
const retired = slot?.retired ?? {};
|
|
2132
|
+
if (slot) slot.retired = {};
|
|
2133
|
+
const { common, sessionId } = await openCall(ctx, toolName, extra, params, retired);
|
|
2064
2134
|
const terminate = async (build, replacement) => {
|
|
2065
2135
|
if (!slot) {
|
|
2066
2136
|
await emit(ctx.sink, build);
|
|
@@ -2228,10 +2298,28 @@ function extractGoalParam(args, paramName, toolName, dispositions, registered) {
|
|
|
2228
2298
|
`
|
|
2229
2299
|
);
|
|
2230
2300
|
}
|
|
2301
|
+
return popText(args, paramName);
|
|
2302
|
+
}
|
|
2303
|
+
function popText(args, paramName) {
|
|
2304
|
+
if (!(paramName in args)) return null;
|
|
2231
2305
|
const raw = args[paramName];
|
|
2232
2306
|
delete args[paramName];
|
|
2233
2307
|
return typeof raw === "string" && raw.trim() ? raw : null;
|
|
2234
2308
|
}
|
|
2309
|
+
function stripRetiredParams(params, ctx) {
|
|
2310
|
+
const toolName = params?.name;
|
|
2311
|
+
const args = params?.arguments;
|
|
2312
|
+
if (typeof toolName !== "string") return {};
|
|
2313
|
+
if (toolName === ctx.annotationToolName) return {};
|
|
2314
|
+
if (typeof args !== "object" || args === null || Array.isArray(args)) return {};
|
|
2315
|
+
const dispositions = ctx.paramRegistry.get(toolName);
|
|
2316
|
+
const retired = {};
|
|
2317
|
+
for (const name of RETIRED_PARAM_NAMES) {
|
|
2318
|
+
if (dispositions === void 0 || dispositions[name] === "native" || !(name in args)) continue;
|
|
2319
|
+
retired[name] = popText(args, name);
|
|
2320
|
+
}
|
|
2321
|
+
return retired;
|
|
2322
|
+
}
|
|
2235
2323
|
var MAX_REMEMBERED_SURFACES = 1024;
|
|
2236
2324
|
async function maybeEmitSurfaceSnapshot(ctx) {
|
|
2237
2325
|
if (!ctx.surfaceState.dirty) return;
|
|
@@ -2251,10 +2339,11 @@ async function maybeEmitSurfaceSnapshot(ctx) {
|
|
|
2251
2339
|
if (emitted.size >= MAX_REMEMBERED_SURFACES) emitted.clear();
|
|
2252
2340
|
emitted.add(digest);
|
|
2253
2341
|
const seam = buildSeamAugmentations({
|
|
2254
|
-
injectedToolNames: [ctx.annotationToolName],
|
|
2255
|
-
intentParamNames: [USER_GOAL_PARAM_NAME
|
|
2342
|
+
injectedToolNames: ctx.annotationToolName ? [ctx.annotationToolName] : [],
|
|
2343
|
+
intentParamNames: [USER_GOAL_PARAM_NAME],
|
|
2256
2344
|
intentParamMode: ctx.intentParamMode,
|
|
2257
|
-
requiredParamNames: requiredParamNames(ctx.intentParamMode)
|
|
2345
|
+
requiredParamNames: requiredParamNames(ctx.intentParamMode),
|
|
2346
|
+
instructionsSuffix: instructionsSuffixServed(ctx.annotationToolName, ctx.servedInstructions())
|
|
2258
2347
|
});
|
|
2259
2348
|
try {
|
|
2260
2349
|
await ctx.emitSurface(ctx.fallbackSessionId, digest, { ...snapshot, seam_augmentations: seam });
|
|
@@ -2265,11 +2354,19 @@ async function maybeEmitSurfaceSnapshot(ctx) {
|
|
|
2265
2354
|
ctx.surfaceState.dirty = true;
|
|
2266
2355
|
}
|
|
2267
2356
|
}
|
|
2357
|
+
function declaredIntentParams(vendorSchema) {
|
|
2358
|
+
const declared2 = {};
|
|
2359
|
+
for (const name of [USER_GOAL_PARAM_NAME, ...RETIRED_PARAM_NAMES]) {
|
|
2360
|
+
if (declaresParam(vendorSchema, name)) declared2[name] = "native";
|
|
2361
|
+
}
|
|
2362
|
+
return declared2;
|
|
2363
|
+
}
|
|
2268
2364
|
function captureAndInject(name, entry, ctx) {
|
|
2269
2365
|
if (!entry || typeof entry !== "object") return;
|
|
2270
2366
|
const mutable = entry;
|
|
2271
|
-
|
|
2272
|
-
|
|
2367
|
+
const vendorSchema = ctx.vendorToolJsonSchema(name, mutable.inputSchema);
|
|
2368
|
+
ctx.surfaceState.noteTool(name, vendorSchema, mutable.description);
|
|
2369
|
+
if (declaresParam(vendorSchema, USER_GOAL_PARAM_NAME)) ctx.dropSubagentClause(name);
|
|
2273
2370
|
const isV2 = typeof mutable.executor === "function";
|
|
2274
2371
|
try {
|
|
2275
2372
|
const { schema, dispositions } = isV2 ? injectGoalParamsV2(mutable.inputSchema, ctx.intentParamMode) : injectGoalParams(mutable.inputSchema, ctx.intentParamMode);
|
|
@@ -2277,9 +2374,10 @@ function captureAndInject(name, entry, ctx) {
|
|
|
2277
2374
|
mutable.inputSchema = schema;
|
|
2278
2375
|
ctx.bustSchemaMemo(name);
|
|
2279
2376
|
ctx.paramRegistry.set(name, dispositions);
|
|
2280
|
-
if (dispositions[OVERALL_TASK_PARAM_NAME] === "native") ctx.dropSubagentClause(name);
|
|
2281
2377
|
} else {
|
|
2282
|
-
|
|
2378
|
+
const declared2 = declaredIntentParams(vendorSchema);
|
|
2379
|
+
if (Object.keys(declared2).length > 0) ctx.paramRegistry.set(name, declared2);
|
|
2380
|
+
else ctx.paramRegistry.delete(name);
|
|
2283
2381
|
}
|
|
2284
2382
|
} catch {
|
|
2285
2383
|
}
|
|
@@ -2380,7 +2478,7 @@ function installLifecycleSeam(lowLevel, ctx, spec) {
|
|
|
2380
2478
|
async function openLifecycleCall(ctx, spec, requestParams, extra) {
|
|
2381
2479
|
try {
|
|
2382
2480
|
const parts = await resolveEnvelopeParts(ctx, extra);
|
|
2383
|
-
const
|
|
2481
|
+
const caller = await resolveCaller(ctx.resolvePrincipal, {
|
|
2384
2482
|
extra,
|
|
2385
2483
|
toolName: null,
|
|
2386
2484
|
arguments: {}
|
|
@@ -2393,7 +2491,8 @@ async function openLifecycleCall(ctx, spec, requestParams, extra) {
|
|
|
2393
2491
|
vendor_id: ctx.vendorId,
|
|
2394
2492
|
session_id: parts.sessionId,
|
|
2395
2493
|
consent_token: ctx.consentToken,
|
|
2396
|
-
principal,
|
|
2494
|
+
principal: caller.principal,
|
|
2495
|
+
organization: caller.organization,
|
|
2397
2496
|
transport_observed: observeTransport(extra),
|
|
2398
2497
|
session_id_origin: parts.origin,
|
|
2399
2498
|
client_observed: observeClient(extra, ctx),
|
|
@@ -2521,7 +2620,13 @@ function installToolsCallSeam(lowLevel, toolEntry, ctx) {
|
|
|
2521
2620
|
TOOLS_CALL_METHOD,
|
|
2522
2621
|
TOOLS_CALL_SEAM,
|
|
2523
2622
|
(handler) => async (...args) => {
|
|
2524
|
-
const
|
|
2623
|
+
const request = args[0];
|
|
2624
|
+
const slot = {
|
|
2625
|
+
innerFired: false,
|
|
2626
|
+
flushTerminal: null,
|
|
2627
|
+
retired: stripRetiredParams(request?.params, ctx),
|
|
2628
|
+
validation: ctx.validationProbeLive() ? "unseen" : "unprobed"
|
|
2629
|
+
};
|
|
2525
2630
|
let outcome = CLEAN_OUTCOME;
|
|
2526
2631
|
try {
|
|
2527
2632
|
const result = await callSlots.run(slot, () => handler(...args));
|
|
@@ -2536,6 +2641,27 @@ function installToolsCallSeam(lowLevel, toolEntry, ctx) {
|
|
|
2536
2641
|
}
|
|
2537
2642
|
);
|
|
2538
2643
|
}
|
|
2644
|
+
var VALIDATION_PROBE = /* @__PURE__ */ Symbol("baton.validationProbe");
|
|
2645
|
+
function installValidationProbe(server) {
|
|
2646
|
+
const target = server;
|
|
2647
|
+
const original = target.validateToolInput;
|
|
2648
|
+
if (typeof original !== "function") return () => false;
|
|
2649
|
+
if (original[VALIDATION_PROBE]) return () => target.validateToolInput === original;
|
|
2650
|
+
const probe = async function(...args) {
|
|
2651
|
+
const slot = callSlots.getStore();
|
|
2652
|
+
try {
|
|
2653
|
+
const parsed = await original.apply(this, args);
|
|
2654
|
+
if (slot) slot.validation = "passed";
|
|
2655
|
+
return parsed;
|
|
2656
|
+
} catch (err) {
|
|
2657
|
+
if (slot) slot.validation = "rejected";
|
|
2658
|
+
throw err;
|
|
2659
|
+
}
|
|
2660
|
+
};
|
|
2661
|
+
Object.assign(probe, { [VALIDATION_PROBE]: true });
|
|
2662
|
+
target.validateToolInput = probe;
|
|
2663
|
+
return () => target.validateToolInput === probe;
|
|
2664
|
+
}
|
|
2539
2665
|
async function settleCall(slot, outcome, args, toolEntry, ctx) {
|
|
2540
2666
|
try {
|
|
2541
2667
|
if (slot.flushTerminal !== null) {
|
|
@@ -2546,13 +2672,13 @@ async function settleCall(slot, outcome, args, toolEntry, ctx) {
|
|
|
2546
2672
|
}
|
|
2547
2673
|
if (slot.innerFired) return;
|
|
2548
2674
|
if (!outcome.failed) return;
|
|
2549
|
-
await emitRequestSideFailure(outcome, args, toolEntry, ctx);
|
|
2675
|
+
await emitRequestSideFailure(outcome, args, slot, toolEntry, ctx);
|
|
2550
2676
|
} catch (err) {
|
|
2551
2677
|
process.stderr.write(`baton: settling a tool call failed; events dropped: ${String(err)}
|
|
2552
2678
|
`);
|
|
2553
2679
|
}
|
|
2554
2680
|
}
|
|
2555
|
-
async function emitRequestSideFailure(outcome, args, toolEntry, ctx) {
|
|
2681
|
+
async function emitRequestSideFailure(outcome, args, slot, toolEntry, ctx) {
|
|
2556
2682
|
const request = args[0];
|
|
2557
2683
|
const toolName = request?.params?.name;
|
|
2558
2684
|
if (typeof toolName !== "string") return;
|
|
@@ -2565,9 +2691,11 @@ async function emitRequestSideFailure(outcome, args, toolEntry, ctx) {
|
|
|
2565
2691
|
toolName,
|
|
2566
2692
|
args[1],
|
|
2567
2693
|
params,
|
|
2694
|
+
slot.retired,
|
|
2568
2695
|
entry != null
|
|
2569
2696
|
);
|
|
2570
|
-
const failureKind = requestSideFailureKind(entry);
|
|
2697
|
+
const failureKind = requestSideFailureKind(entry, slot.validation);
|
|
2698
|
+
const text = () => outcome.raised ? messageOf(outcome.err) : errorText(outcome.result);
|
|
2571
2699
|
await emit(
|
|
2572
2700
|
ctx.sink,
|
|
2573
2701
|
() => ToolCallErrorEventSchema.parse({
|
|
@@ -2588,8 +2716,8 @@ async function emitRequestSideFailure(outcome, args, toolEntry, ctx) {
|
|
|
2588
2716
|
error_type: outcome.raised ? errorTypeOf(outcome.err) : TOOL_ERROR_TYPE,
|
|
2589
2717
|
// ⚠ `null`, not 0. The vendor's handler never ran, so there is no
|
|
2590
2718
|
// handler duration to report; the time this call did spend was spent
|
|
2591
|
-
//
|
|
2592
|
-
//
|
|
2719
|
+
// above the handler, which is not what any other event means by this
|
|
2720
|
+
// member. §11.4 types it nullable for cases like this one.
|
|
2593
2721
|
duration_ms: null,
|
|
2594
2722
|
// ⚠ A CONDITIONAL SPREAD, not `failure_kind: failureKind`. Zod's
|
|
2595
2723
|
// `.optional()` accepts `undefined` and keeps the KEY in its output,
|
|
@@ -2599,11 +2727,13 @@ async function emitRequestSideFailure(outcome, args, toolEntry, ctx) {
|
|
|
2599
2727
|
// determine the kind MUST OMIT the member, and omitting is what this
|
|
2600
2728
|
// spells.
|
|
2601
2729
|
...failureKind === void 0 ? {} : { failure_kind: failureKind },
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2730
|
+
...requestSideFailureFields(
|
|
2731
|
+
failureKind,
|
|
2732
|
+
ctx.resultCaptureMode,
|
|
2733
|
+
text,
|
|
2734
|
+
outcome.raised ? null : outcome.result,
|
|
2735
|
+
(value) => errorBody(ctx, value),
|
|
2736
|
+
ctx.scrubber
|
|
2607
2737
|
)
|
|
2608
2738
|
}
|
|
2609
2739
|
})
|
|
@@ -2684,7 +2814,9 @@ function withBaton(server, supplied = {}) {
|
|
|
2684
2814
|
annotationToolName: ""
|
|
2685
2815
|
});
|
|
2686
2816
|
}
|
|
2687
|
-
|
|
2817
|
+
const resolved = resolveBatonConfig(supplied);
|
|
2818
|
+
warnUnusedReportSettings(resolved);
|
|
2819
|
+
return install(server, supplied, resolved, /* @__PURE__ */ new Set(), /* @__PURE__ */ new Set());
|
|
2688
2820
|
}
|
|
2689
2821
|
function createBaton(supplied = {}) {
|
|
2690
2822
|
const disabledBy = captureDisabled();
|
|
@@ -2695,7 +2827,8 @@ function createBaton(supplied = {}) {
|
|
|
2695
2827
|
}, flush: () => unused.flush(), aclose: () => unused.aclose() };
|
|
2696
2828
|
}
|
|
2697
2829
|
const resolved = resolveBatonConfig(supplied);
|
|
2698
|
-
|
|
2830
|
+
warnUnusedReportSettings(resolved);
|
|
2831
|
+
if (resolved.reportMode === "on") deriveAnnotationToolName(resolved.vendorId, supplied.annotationToolName);
|
|
2699
2832
|
const sink = resolved.sink ?? new StdoutSink();
|
|
2700
2833
|
const config = {
|
|
2701
2834
|
...resolved,
|
|
@@ -2703,23 +2836,25 @@ function createBaton(supplied = {}) {
|
|
|
2703
2836
|
tenantId: resolveTenantId(resolved.tenantId, resolved.vendorId)
|
|
2704
2837
|
};
|
|
2705
2838
|
const emittedSurfaceHashes = /* @__PURE__ */ new Set();
|
|
2839
|
+
const warned3 = /* @__PURE__ */ new Set();
|
|
2706
2840
|
return {
|
|
2707
2841
|
wrap: (server) => {
|
|
2708
|
-
install(server, supplied, config, emittedSurfaceHashes);
|
|
2842
|
+
install(server, supplied, config, emittedSurfaceHashes, warned3);
|
|
2709
2843
|
},
|
|
2710
2844
|
flush: () => sink.flush(),
|
|
2711
2845
|
aclose: () => sink.aclose()
|
|
2712
2846
|
};
|
|
2713
2847
|
}
|
|
2714
|
-
function install(server, supplied, resolved, emittedSurfaceHashes) {
|
|
2848
|
+
function install(server, supplied, resolved, emittedSurfaceHashes, warned3) {
|
|
2715
2849
|
const internals = server;
|
|
2716
2850
|
const serverName = usableServerName(() => ({
|
|
2717
2851
|
name: internals.server._serverInfo?.name,
|
|
2718
2852
|
className: internals.constructor.name
|
|
2719
2853
|
}));
|
|
2720
|
-
const config = withServerDisplayName(resolved, supplied, serverName);
|
|
2854
|
+
const config = resolved.reportMode === "on" ? withServerDisplayName(resolved, supplied, serverName) : resolved;
|
|
2721
2855
|
const sink = config.sink ?? new StdoutSink();
|
|
2722
|
-
const
|
|
2856
|
+
const reports = reportsFor(config);
|
|
2857
|
+
const annotationToolName = reports ? resolveAnnotationToolName(serverName, { ...config, vendorDisplayName: reports.vendorDisplayName }) : "";
|
|
2723
2858
|
const intentParamMode = config.intentParamMode ?? "required";
|
|
2724
2859
|
const counter2 = new SessionCounter();
|
|
2725
2860
|
const fallbackSessionId = `sdk-${uuid7()}`;
|
|
@@ -2751,12 +2886,23 @@ function install(server, supplied, resolved, emittedSurfaceHashes) {
|
|
|
2751
2886
|
const memo = internals._toolInputSchemaJson;
|
|
2752
2887
|
if (memo && typeof memo === "object") delete memo[name];
|
|
2753
2888
|
};
|
|
2889
|
+
const warnOnce2 = (key, message) => {
|
|
2890
|
+
if (warned3.has(key)) return;
|
|
2891
|
+
warned3.add(key);
|
|
2892
|
+
process.stderr.write(message);
|
|
2893
|
+
};
|
|
2754
2894
|
const dropSubagentClause = (name) => {
|
|
2755
2895
|
const current2 = internals.server._instructions;
|
|
2756
|
-
if (
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2896
|
+
if (current2 === INTENT_INSTRUCTIONS) {
|
|
2897
|
+
internals.server._instructions = "";
|
|
2898
|
+
} else if (typeof current2 === "string" && current2.includes(INSTRUCTIONS_SUBAGENT_CLAUSE)) {
|
|
2899
|
+
internals.server._instructions = current2.replace(INSTRUCTIONS_SUBAGENT_CLAUSE, "");
|
|
2900
|
+
} else {
|
|
2901
|
+
return;
|
|
2902
|
+
}
|
|
2903
|
+
warnOnce2(
|
|
2904
|
+
"native-user-goal",
|
|
2905
|
+
`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
2906
|
`
|
|
2761
2907
|
);
|
|
2762
2908
|
};
|
|
@@ -2793,20 +2939,22 @@ function install(server, supplied, resolved, emittedSurfaceHashes) {
|
|
|
2793
2939
|
annotationToolName,
|
|
2794
2940
|
intentParamMode,
|
|
2795
2941
|
resultCaptureMode: config.resultCaptureMode ?? "full",
|
|
2942
|
+
validationProbeLive: installValidationProbe(server),
|
|
2796
2943
|
paramRegistry: /* @__PURE__ */ new Map(),
|
|
2797
2944
|
resolvePrincipal: config.resolvePrincipal,
|
|
2798
2945
|
vendorToolJsonSchema,
|
|
2799
2946
|
bustSchemaMemo,
|
|
2800
2947
|
dropSubagentClause,
|
|
2948
|
+
servedInstructions: () => internals.server._instructions,
|
|
2801
2949
|
surfaceState,
|
|
2802
2950
|
emitSurface
|
|
2803
2951
|
};
|
|
2804
|
-
const
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2952
|
+
const ours = reports ? buildServerInstructions({ vendorDisplayName: reports.vendorDisplayName, annotationToolName }) : INTENT_INSTRUCTIONS;
|
|
2953
|
+
internals.server._instructions = withVendorInstructions(
|
|
2954
|
+
internals.server,
|
|
2955
|
+
ours,
|
|
2956
|
+
(message) => warnOnce2("instructions-length", message)
|
|
2957
|
+
);
|
|
2810
2958
|
installToolsListSeam(internals.server, ctx);
|
|
2811
2959
|
installToolsCallSeam(
|
|
2812
2960
|
internals.server,
|
|
@@ -2824,12 +2972,12 @@ function install(server, supplied, resolved, emittedSurfaceHashes) {
|
|
|
2824
2972
|
ctx
|
|
2825
2973
|
);
|
|
2826
2974
|
installLifecycleSeams(internals.server, ctx);
|
|
2827
|
-
const resolvedAnnotationToolName = registerAnnotationTool(server, {
|
|
2975
|
+
const resolvedAnnotationToolName = !reports ? "" : registerAnnotationTool(server, {
|
|
2828
2976
|
sink,
|
|
2829
2977
|
counter: ctx.counter,
|
|
2830
2978
|
tenantId: ctx.tenantId,
|
|
2831
2979
|
vendorId: ctx.vendorId,
|
|
2832
|
-
vendorDisplayName:
|
|
2980
|
+
vendorDisplayName: reports.vendorDisplayName,
|
|
2833
2981
|
consentToken: ctx.consentToken,
|
|
2834
2982
|
resolvePrincipal: ctx.resolvePrincipal,
|
|
2835
2983
|
fallbackSessionId: ctx.fallbackSessionId,
|
|
@@ -2858,6 +3006,18 @@ function install(server, supplied, resolved, emittedSurfaceHashes) {
|
|
|
2858
3006
|
annotationToolName: resolvedAnnotationToolName
|
|
2859
3007
|
});
|
|
2860
3008
|
}
|
|
3009
|
+
function reportsFor(config) {
|
|
3010
|
+
if (config.reportMode !== "on") return void 0;
|
|
3011
|
+
return { vendorDisplayName: config.vendorDisplayName ?? "" };
|
|
3012
|
+
}
|
|
3013
|
+
function warnUnusedReportSettings(config) {
|
|
3014
|
+
if (config.reportMode !== "on" && config.annotationToolName !== void 0) {
|
|
3015
|
+
process.stderr.write(
|
|
3016
|
+
`baton: annotationToolName=${JSON.stringify(config.annotationToolName)} has no effect with reportMode: "off", because no annotation tool is registered
|
|
3017
|
+
`
|
|
3018
|
+
);
|
|
3019
|
+
}
|
|
3020
|
+
}
|
|
2861
3021
|
|
|
2862
3022
|
// src/integrations/mcp/oauthHooks.ts
|
|
2863
3023
|
function claimsOf(context) {
|
|
@@ -2900,6 +3060,7 @@ function sessionEnvelope(session) {
|
|
|
2900
3060
|
session_id_origin: session.origin,
|
|
2901
3061
|
transport_observed: TRANSPORT_LIBRARY,
|
|
2902
3062
|
principal: session.principal,
|
|
3063
|
+
organization: session.organization,
|
|
2903
3064
|
client_observed: session.clientObserved,
|
|
2904
3065
|
runtime_meta: session.turnId !== null ? { [TURN_ID_META_KEY]: session.turnId } : null
|
|
2905
3066
|
};
|
|
@@ -2916,28 +3077,16 @@ function vendorId(name, value) {
|
|
|
2916
3077
|
}
|
|
2917
3078
|
return null;
|
|
2918
3079
|
}
|
|
2919
|
-
function
|
|
2920
|
-
return { sessionId
|
|
2921
|
-
}
|
|
2922
|
-
function wirePrincipal(value) {
|
|
2923
|
-
if (value === void 0 || value === null) return null;
|
|
2924
|
-
const principal = normalizePrincipal(value);
|
|
2925
|
-
if (principal === null) {
|
|
2926
|
-
warn3("principal was given but has no usable principalId \u2014 sending none.");
|
|
2927
|
-
return null;
|
|
2928
|
-
}
|
|
2929
|
-
return principalFor(
|
|
2930
|
-
principal,
|
|
2931
|
-
(form) => warn3(`principal form ${JSON.stringify(form)} is not registered \u2014 sending "raw".`)
|
|
2932
|
-
);
|
|
3080
|
+
function blank(sessionId, origin) {
|
|
3081
|
+
return { sessionId, origin, ...NO_CALLER, clientObserved: null, turnId: null };
|
|
2933
3082
|
}
|
|
2934
3083
|
function newSession(values, scrubber, disabled) {
|
|
2935
3084
|
const id = vendorId("sessionId", values.sessionId);
|
|
2936
|
-
const base = id !== null ?
|
|
3085
|
+
const base = id !== null ? blank(id, "observed") : blank(uuid7(), "minted");
|
|
2937
3086
|
if (disabled) return base;
|
|
2938
3087
|
return {
|
|
2939
3088
|
...base,
|
|
2940
|
-
|
|
3089
|
+
...callerFor(values.principal, (_key, message) => warn3(`principal ${message}`)),
|
|
2941
3090
|
clientObserved: clientObservedFrom(values.clientInfo, values.headers, scrubber),
|
|
2942
3091
|
turnId: vendorId("turnId", values.turnId)
|
|
2943
3092
|
};
|
|
@@ -2948,7 +3097,7 @@ function runInSession(owner, session, fn) {
|
|
|
2948
3097
|
}
|
|
2949
3098
|
function sessionForCall(owner) {
|
|
2950
3099
|
const entry = current.getStore();
|
|
2951
|
-
return entry !== void 0 && entry.owner === owner ? entry.session :
|
|
3100
|
+
return entry !== void 0 && entry.owner === owner ? entry.session : blank(uuid7(), "minted");
|
|
2952
3101
|
}
|
|
2953
3102
|
var lastSequence = 0;
|
|
2954
3103
|
function nextSequence() {
|
|
@@ -3377,6 +3526,7 @@ export {
|
|
|
3377
3526
|
EventSchema,
|
|
3378
3527
|
EventTypeSchema,
|
|
3379
3528
|
HttpSink,
|
|
3529
|
+
INTENT_INSTRUCTIONS,
|
|
3380
3530
|
INTENT_PARAMS,
|
|
3381
3531
|
PRINCIPAL_FORM_HASHED,
|
|
3382
3532
|
PRINCIPAL_FORM_RAW,
|