@goodtiming/baton-sdk 0.5.2 → 0.5.3
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 +57 -2
- package/dist/index.cjs +292 -150
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +607 -30
- package/dist/index.d.ts +607 -30
- package/dist/index.js +283 -148
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -77,6 +77,13 @@ __export(index_exports, {
|
|
|
77
77
|
ToolCallErrorPayloadSchema: () => ToolCallErrorPayloadSchema,
|
|
78
78
|
ToolCallStartEventSchema: () => ToolCallStartEventSchema,
|
|
79
79
|
ToolCallStartPayloadSchema: () => ToolCallStartPayloadSchema,
|
|
80
|
+
ToolListEndEventSchema: () => ToolListEndEventSchema,
|
|
81
|
+
ToolListEndPayloadSchema: () => ToolListEndPayloadSchema,
|
|
82
|
+
ToolListErrorEventSchema: () => ToolListErrorEventSchema,
|
|
83
|
+
ToolListErrorPayloadSchema: () => ToolListErrorPayloadSchema,
|
|
84
|
+
ToolListStartEventSchema: () => ToolListStartEventSchema,
|
|
85
|
+
ToolListStartPayloadSchema: () => ToolListStartPayloadSchema,
|
|
86
|
+
createBaton: () => createBaton,
|
|
80
87
|
identityScrub: () => identityScrub,
|
|
81
88
|
principalFromOAuthEmail: () => principalFromOAuthEmail,
|
|
82
89
|
principalFromOAuthSub: () => principalFromOAuthSub,
|
|
@@ -86,11 +93,36 @@ __export(index_exports, {
|
|
|
86
93
|
module.exports = __toCommonJS(index_exports);
|
|
87
94
|
|
|
88
95
|
// src/version.ts
|
|
89
|
-
var SDK_VERSION = "ts-0.5.
|
|
96
|
+
var SDK_VERSION = "ts-0.5.3";
|
|
90
97
|
|
|
91
98
|
// src/events.ts
|
|
92
99
|
var import_zod = require("zod");
|
|
93
|
-
|
|
100
|
+
|
|
101
|
+
// src/uuid7.ts
|
|
102
|
+
var import_node_crypto = require("crypto");
|
|
103
|
+
var COUNTER_MAX = 4095;
|
|
104
|
+
var lastMs = -1;
|
|
105
|
+
var counter = 0;
|
|
106
|
+
function uuid7() {
|
|
107
|
+
const bytes = (0, import_node_crypto.randomBytes)(18);
|
|
108
|
+
const seed = (bytes[16] << 8 | bytes[17]) & 2047;
|
|
109
|
+
const now = Date.now();
|
|
110
|
+
if (now > lastMs) {
|
|
111
|
+
lastMs = now;
|
|
112
|
+
counter = seed;
|
|
113
|
+
} else if (++counter > COUNTER_MAX) {
|
|
114
|
+
lastMs += 1;
|
|
115
|
+
counter = seed;
|
|
116
|
+
}
|
|
117
|
+
bytes.writeUIntBE(lastMs, 0, 6);
|
|
118
|
+
bytes[6] = 112 | counter >> 8;
|
|
119
|
+
bytes[7] = counter & 255;
|
|
120
|
+
bytes[8] = 128 | bytes[8] & 63;
|
|
121
|
+
const hex = bytes.toString("hex", 0, 16);
|
|
122
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/events.ts
|
|
94
126
|
var ToolCallStartPayloadSchema = import_zod.z.object({
|
|
95
127
|
tool_name: import_zod.z.string(),
|
|
96
128
|
params: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).default({}),
|
|
@@ -183,6 +215,10 @@ var SurfaceSnapshotPayloadSchema = import_zod.z.object({
|
|
|
183
215
|
seam_augmentations: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).default({})
|
|
184
216
|
}).strict();
|
|
185
217
|
var DEFAULT_CONSENT_TOKEN = "customer-consented";
|
|
218
|
+
var ClientObservedSchema = import_zod.z.object({
|
|
219
|
+
info: import_zod.z.object({ name: import_zod.z.string().optional(), version: import_zod.z.string().optional() }).strict().optional(),
|
|
220
|
+
headers: import_zod.z.object({ "user-agent": import_zod.z.string().optional(), "x-anthropic-client": import_zod.z.string().optional() }).strict().optional()
|
|
221
|
+
}).strict();
|
|
186
222
|
var PrincipalWireSchema = import_zod.z.object({
|
|
187
223
|
id: import_zod.z.string(),
|
|
188
224
|
source: import_zod.z.string(),
|
|
@@ -190,7 +226,7 @@ var PrincipalWireSchema = import_zod.z.object({
|
|
|
190
226
|
display_name: import_zod.z.string().nullable().optional()
|
|
191
227
|
}).strict();
|
|
192
228
|
var envelopeShape = {
|
|
193
|
-
event_id: import_zod.z.uuid().default(() => (
|
|
229
|
+
event_id: import_zod.z.uuid().default(() => uuid7()),
|
|
194
230
|
tenant_id: import_zod.z.string(),
|
|
195
231
|
vendor_id: import_zod.z.string(),
|
|
196
232
|
session_id: import_zod.z.string(),
|
|
@@ -198,9 +234,12 @@ var envelopeShape = {
|
|
|
198
234
|
captured_at: import_zod.z.string(),
|
|
199
235
|
consent_token: import_zod.z.string(),
|
|
200
236
|
sdk_version: import_zod.z.string().default(SDK_VERSION),
|
|
237
|
+
// Legacy (SPEC §3.4): never set by this SDK, so always the default. The
|
|
238
|
+
// consumer names the client from `client_observed`.
|
|
201
239
|
agent_runtime: import_zod.z.string().default("unknown"),
|
|
202
240
|
principal: PrincipalWireSchema.nullable().default(null),
|
|
203
241
|
transport_observed: import_zod.z.string().nullable().default(null),
|
|
242
|
+
client_observed: ClientObservedSchema.nullable().default(null),
|
|
204
243
|
call_id: import_zod.z.string().nullable().default(null),
|
|
205
244
|
runtime_meta: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).nullable().default(null)
|
|
206
245
|
};
|
|
@@ -224,7 +263,11 @@ var EventTypeSchema = import_zod.z.enum([
|
|
|
224
263
|
"prompt_list_error",
|
|
225
264
|
"prompt_get_start",
|
|
226
265
|
"prompt_get_end",
|
|
227
|
-
"prompt_get_error"
|
|
266
|
+
"prompt_get_error",
|
|
267
|
+
// SPEC §11.4.5.
|
|
268
|
+
"tool_list_start",
|
|
269
|
+
"tool_list_end",
|
|
270
|
+
"tool_list_error"
|
|
228
271
|
]);
|
|
229
272
|
var timingShape = { duration_ms: import_zod.z.number().int().nullable().default(null) };
|
|
230
273
|
var failureShape = { error_type: import_zod.z.string(), error_body: import_zod.z.string(), ...timingShape };
|
|
@@ -263,6 +306,14 @@ var PromptListEndPayloadSchema = import_zod.z.object({
|
|
|
263
306
|
var PromptListErrorPayloadSchema = import_zod.z.object({
|
|
264
307
|
...failureShape
|
|
265
308
|
}).strict();
|
|
309
|
+
var ToolListStartPayloadSchema = import_zod.z.object({}).strict();
|
|
310
|
+
var ToolListEndPayloadSchema = import_zod.z.object({
|
|
311
|
+
count: import_zod.z.number().int(),
|
|
312
|
+
...timingShape
|
|
313
|
+
}).strict();
|
|
314
|
+
var ToolListErrorPayloadSchema = import_zod.z.object({
|
|
315
|
+
...failureShape
|
|
316
|
+
}).strict();
|
|
266
317
|
var PromptGetStartPayloadSchema = import_zod.z.object({
|
|
267
318
|
name: import_zod.z.string(),
|
|
268
319
|
/** The prompt's arguments, PII-scrubbed (SPEC §7). `{}` from this producer
|
|
@@ -278,18 +329,22 @@ var PromptGetErrorPayloadSchema = import_zod.z.object({
|
|
|
278
329
|
name: import_zod.z.string(),
|
|
279
330
|
...failureShape
|
|
280
331
|
}).strict();
|
|
332
|
+
var REQUIRED_CALL_ID = import_zod.z.string().min(1);
|
|
281
333
|
var ToolCallStartEventSchema = import_zod.z.object({
|
|
282
334
|
...envelopeShape,
|
|
335
|
+
call_id: REQUIRED_CALL_ID,
|
|
283
336
|
event_type: import_zod.z.literal("tool_call_start").default("tool_call_start"),
|
|
284
337
|
payload: ToolCallStartPayloadSchema
|
|
285
338
|
}).strict();
|
|
286
339
|
var ToolCallEndEventSchema = import_zod.z.object({
|
|
287
340
|
...envelopeShape,
|
|
341
|
+
call_id: REQUIRED_CALL_ID,
|
|
288
342
|
event_type: import_zod.z.literal("tool_call_end").default("tool_call_end"),
|
|
289
343
|
payload: ToolCallEndPayloadSchema
|
|
290
344
|
}).strict();
|
|
291
345
|
var ToolCallErrorEventSchema = import_zod.z.object({
|
|
292
346
|
...envelopeShape,
|
|
347
|
+
call_id: REQUIRED_CALL_ID,
|
|
293
348
|
event_type: import_zod.z.literal("tool_call_error").default("tool_call_error"),
|
|
294
349
|
payload: ToolCallErrorPayloadSchema
|
|
295
350
|
}).strict();
|
|
@@ -363,6 +418,21 @@ var PromptGetErrorEventSchema = import_zod.z.object({
|
|
|
363
418
|
event_type: import_zod.z.literal("prompt_get_error").default("prompt_get_error"),
|
|
364
419
|
payload: PromptGetErrorPayloadSchema
|
|
365
420
|
}).strict();
|
|
421
|
+
var ToolListStartEventSchema = import_zod.z.object({
|
|
422
|
+
...envelopeShape,
|
|
423
|
+
event_type: import_zod.z.literal("tool_list_start").default("tool_list_start"),
|
|
424
|
+
payload: ToolListStartPayloadSchema
|
|
425
|
+
}).strict();
|
|
426
|
+
var ToolListEndEventSchema = import_zod.z.object({
|
|
427
|
+
...envelopeShape,
|
|
428
|
+
event_type: import_zod.z.literal("tool_list_end").default("tool_list_end"),
|
|
429
|
+
payload: ToolListEndPayloadSchema
|
|
430
|
+
}).strict();
|
|
431
|
+
var ToolListErrorEventSchema = import_zod.z.object({
|
|
432
|
+
...envelopeShape,
|
|
433
|
+
event_type: import_zod.z.literal("tool_list_error").default("tool_list_error"),
|
|
434
|
+
payload: ToolListErrorPayloadSchema
|
|
435
|
+
}).strict();
|
|
366
436
|
var EventSchema = import_zod.z.discriminatedUnion("event_type", [
|
|
367
437
|
ToolCallStartEventSchema,
|
|
368
438
|
ToolCallEndEventSchema,
|
|
@@ -380,7 +450,10 @@ var EventSchema = import_zod.z.discriminatedUnion("event_type", [
|
|
|
380
450
|
PromptListErrorEventSchema,
|
|
381
451
|
PromptGetStartEventSchema,
|
|
382
452
|
PromptGetEndEventSchema,
|
|
383
|
-
PromptGetErrorEventSchema
|
|
453
|
+
PromptGetErrorEventSchema,
|
|
454
|
+
ToolListStartEventSchema,
|
|
455
|
+
ToolListEndEventSchema,
|
|
456
|
+
ToolListErrorEventSchema
|
|
384
457
|
]);
|
|
385
458
|
|
|
386
459
|
// src/scrub.ts
|
|
@@ -676,7 +749,6 @@ var HttpSink = class _HttpSink {
|
|
|
676
749
|
|
|
677
750
|
// src/integrations/mcp/withBaton.ts
|
|
678
751
|
var import_node_async_hooks = require("async_hooks");
|
|
679
|
-
var import_uuid2 = require("uuid");
|
|
680
752
|
|
|
681
753
|
// src/integrations/mcp/annotation.ts
|
|
682
754
|
var import_zod2 = require("zod");
|
|
@@ -829,7 +901,7 @@ function buildServerInstructions(options) {
|
|
|
829
901
|
`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.`
|
|
830
902
|
);
|
|
831
903
|
}
|
|
832
|
-
return rendered;
|
|
904
|
+
return options.intentParamMode === "off" ? rendered : rendered + INSTRUCTIONS_SUBAGENT_CLAUSE;
|
|
833
905
|
}
|
|
834
906
|
function buildAnnotationToolDescription(options) {
|
|
835
907
|
return ANNOTATION_TOOL_DESCRIPTION_TEMPLATE(options.vendorDisplayName);
|
|
@@ -841,16 +913,24 @@ var INTENT_SOURCE_PARAM = "injected_param";
|
|
|
841
913
|
var USER_GOAL_PARAM_BODY = "One sentence: what the user is actually trying to accomplish with this call (their goal, not a restatement of the arguments).";
|
|
842
914
|
var USER_GOAL_PARAM_DESCRIPTION = "OPTIONAL. " + USER_GOAL_PARAM_BODY;
|
|
843
915
|
var USER_GOAL_PARAM_DESCRIPTION_REQUIRED = "REQUIRED. " + USER_GOAL_PARAM_BODY;
|
|
844
|
-
var
|
|
845
|
-
var
|
|
916
|
+
var EXPECTED_RESULT_PARAM_BODY = "One sentence: what a successful result should look like, so a silent/thin failure can be told apart from success.";
|
|
917
|
+
var EXPECTED_RESULT_PARAM_DESCRIPTION = "OPTIONAL. " + EXPECTED_RESULT_PARAM_BODY;
|
|
918
|
+
var EXPECTED_RESULT_PARAM_DESCRIPTION_REQUIRED = "REQUIRED. " + EXPECTED_RESULT_PARAM_BODY;
|
|
919
|
+
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.";
|
|
920
|
+
var OVERALL_TASK_PARAM_DESCRIPTION = "OPTIONAL. " + OVERALL_TASK_PARAM_BODY;
|
|
921
|
+
var OVERALL_TASK_PARAM_DESCRIPTION_REQUIRED = "REQUIRED. " + OVERALL_TASK_PARAM_BODY;
|
|
922
|
+
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.";
|
|
846
923
|
function buildUserGoalParamDescription(options = {}) {
|
|
847
924
|
return options.intentParamMode === "required" ? USER_GOAL_PARAM_DESCRIPTION_REQUIRED : USER_GOAL_PARAM_DESCRIPTION;
|
|
848
925
|
}
|
|
849
|
-
function buildOverallTaskParamDescription() {
|
|
850
|
-
return OVERALL_TASK_PARAM_DESCRIPTION;
|
|
926
|
+
function buildOverallTaskParamDescription(options = {}) {
|
|
927
|
+
return options.intentParamMode === "required" ? OVERALL_TASK_PARAM_DESCRIPTION_REQUIRED : OVERALL_TASK_PARAM_DESCRIPTION;
|
|
928
|
+
}
|
|
929
|
+
function buildExpectedResultParamDescription(options = {}) {
|
|
930
|
+
return options.intentParamMode === "required" ? EXPECTED_RESULT_PARAM_DESCRIPTION_REQUIRED : EXPECTED_RESULT_PARAM_DESCRIPTION;
|
|
851
931
|
}
|
|
852
|
-
function
|
|
853
|
-
return
|
|
932
|
+
function requiredParamNames(intentParamMode) {
|
|
933
|
+
return intentParamMode === "required" ? [USER_GOAL_PARAM_NAME, EXPECTED_RESULT_PARAM_NAME, OVERALL_TASK_PARAM_NAME] : [];
|
|
854
934
|
}
|
|
855
935
|
|
|
856
936
|
// src/integrations/mcp/annotationName.ts
|
|
@@ -992,6 +1072,9 @@ function extraHeaders(extra) {
|
|
|
992
1072
|
}
|
|
993
1073
|
return null;
|
|
994
1074
|
}
|
|
1075
|
+
function isThenable(value) {
|
|
1076
|
+
return typeof value?.then === "function";
|
|
1077
|
+
}
|
|
995
1078
|
|
|
996
1079
|
// src/integrations/mcp/principalResolution.ts
|
|
997
1080
|
function buildPrincipalResolutionContext(extra, toolName, args) {
|
|
@@ -1003,6 +1086,26 @@ function buildPrincipalResolutionContext(extra, toolName, args) {
|
|
|
1003
1086
|
authInfo: extraAuthInfo(extra)
|
|
1004
1087
|
};
|
|
1005
1088
|
}
|
|
1089
|
+
var HOOK_TIMEOUT_MS = 5e3;
|
|
1090
|
+
var TIMED_OUT = /* @__PURE__ */ Symbol("hookTimedOut");
|
|
1091
|
+
async function withinHookBudget(answer) {
|
|
1092
|
+
if (!isThenable(answer)) return answer;
|
|
1093
|
+
let timer;
|
|
1094
|
+
const deadline = new Promise((resolve) => {
|
|
1095
|
+
timer = setTimeout(() => resolve(TIMED_OUT), HOOK_TIMEOUT_MS);
|
|
1096
|
+
timer.unref?.();
|
|
1097
|
+
});
|
|
1098
|
+
try {
|
|
1099
|
+
const first = await Promise.race([answer, deadline]);
|
|
1100
|
+
if (first !== TIMED_OUT) return first;
|
|
1101
|
+
warn(
|
|
1102
|
+
`baton: resolvePrincipal did not answer within ${String(HOOK_TIMEOUT_MS / 1e3)}s; this request is sent without a principal.`
|
|
1103
|
+
);
|
|
1104
|
+
return null;
|
|
1105
|
+
} finally {
|
|
1106
|
+
clearTimeout(timer);
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1006
1109
|
var warnedPreRenameShape = false;
|
|
1007
1110
|
function warnIfPreRenameShape(result) {
|
|
1008
1111
|
const userId = result?.userId;
|
|
@@ -1016,7 +1119,7 @@ async function resolveCallPrincipal(hook, call) {
|
|
|
1016
1119
|
if (hook === void 0) return null;
|
|
1017
1120
|
try {
|
|
1018
1121
|
const context = buildPrincipalResolutionContext(call.extra, call.toolName, call.arguments);
|
|
1019
|
-
const result = await hook(context);
|
|
1122
|
+
const result = await withinHookBudget(hook(context));
|
|
1020
1123
|
const principal = normalizePrincipal(result);
|
|
1021
1124
|
if (principal === null) {
|
|
1022
1125
|
warnIfPreRenameShape(result);
|
|
@@ -1053,56 +1156,67 @@ function scrubOrNull(scrubber, value, field) {
|
|
|
1053
1156
|
}
|
|
1054
1157
|
}
|
|
1055
1158
|
|
|
1056
|
-
// src/integrations/mcp/
|
|
1159
|
+
// src/integrations/mcp/clientObserved.ts
|
|
1057
1160
|
var CLIENT_INFO_META_KEY = "io.modelcontextprotocol/clientInfo";
|
|
1058
|
-
var
|
|
1059
|
-
var
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
if (
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
function declaredOnRequest(meta, envelope) {
|
|
1072
|
-
for (const source of [envelope, meta]) {
|
|
1073
|
-
if (!source) continue;
|
|
1074
|
-
const info = source[CLIENT_INFO_META_KEY];
|
|
1075
|
-
if (info && typeof info === "object") {
|
|
1076
|
-
const name = info.name;
|
|
1077
|
-
if (name !== void 0) return name;
|
|
1078
|
-
}
|
|
1161
|
+
var OBSERVED_HEADERS = ["user-agent", "x-anthropic-client"];
|
|
1162
|
+
var CLIENT_INFO_MAX_LEN = 128;
|
|
1163
|
+
var HEADER_VALUE_MAX_LEN = 256;
|
|
1164
|
+
var hasText = (value) => typeof value === "string" && value !== "";
|
|
1165
|
+
function declared(info) {
|
|
1166
|
+
if (!info || typeof info !== "object") return void 0;
|
|
1167
|
+
const { name, version } = info;
|
|
1168
|
+
return hasText(name) || hasText(version) ? { name, version } : void 0;
|
|
1169
|
+
}
|
|
1170
|
+
function declaredOnRequest(extra) {
|
|
1171
|
+
for (const source of [extraEnvelope(extra), extraMeta(extra)]) {
|
|
1172
|
+
const found = declared(source?.[CLIENT_INFO_META_KEY]);
|
|
1173
|
+
if (found) return found;
|
|
1079
1174
|
}
|
|
1080
1175
|
return void 0;
|
|
1081
1176
|
}
|
|
1082
|
-
function declaredOnConnection(
|
|
1177
|
+
function declaredOnConnection(server) {
|
|
1178
|
+
if (!server) return void 0;
|
|
1179
|
+
const inner = server.server;
|
|
1180
|
+
const holder = inner ?? server;
|
|
1181
|
+
if (typeof holder.getClientVersion !== "function") return void 0;
|
|
1182
|
+
return declared(holder.getClientVersion());
|
|
1183
|
+
}
|
|
1184
|
+
function observedInfo(extra, options) {
|
|
1185
|
+
const sent = declaredOnRequest(extra) ?? declaredOnConnection(options.server);
|
|
1186
|
+
const info = {};
|
|
1187
|
+
const name = clean(sent?.name, options.scrubber, CLIENT_INFO_MAX_LEN);
|
|
1188
|
+
const version = clean(sent?.version, options.scrubber, CLIENT_INFO_MAX_LEN);
|
|
1189
|
+
if (name !== void 0) info.name = name;
|
|
1190
|
+
if (version !== void 0) info.version = version;
|
|
1191
|
+
return Object.keys(info).length > 0 ? info : void 0;
|
|
1192
|
+
}
|
|
1193
|
+
function observedHeaders(extra, options) {
|
|
1194
|
+
const sent = extraHeaders(extra);
|
|
1195
|
+
const headers = {};
|
|
1196
|
+
for (const header of OBSERVED_HEADERS) {
|
|
1197
|
+
const value = clean(sent?.get(header), options.scrubber, HEADER_VALUE_MAX_LEN);
|
|
1198
|
+
if (value !== void 0) headers[header] = value;
|
|
1199
|
+
}
|
|
1200
|
+
return Object.keys(headers).length > 0 ? headers : void 0;
|
|
1201
|
+
}
|
|
1202
|
+
function clean(value, scrubber, max) {
|
|
1203
|
+
if (!hasText(value)) return void 0;
|
|
1204
|
+
const scrubbed = scrubOrNull(scrubber, value, "client_observed");
|
|
1205
|
+
if (!hasText(scrubbed)) return void 0;
|
|
1206
|
+
return capCodePoints(scrubbed, max);
|
|
1207
|
+
}
|
|
1208
|
+
function orNothing(read) {
|
|
1083
1209
|
try {
|
|
1084
|
-
|
|
1085
|
-
if (!server) return void 0;
|
|
1086
|
-
const inner = server.server;
|
|
1087
|
-
const holder = inner ?? server;
|
|
1088
|
-
if (typeof holder.getClientVersion !== "function") return void 0;
|
|
1089
|
-
return holder.getClientVersion()?.name;
|
|
1210
|
+
return read();
|
|
1090
1211
|
} catch {
|
|
1091
1212
|
return void 0;
|
|
1092
1213
|
}
|
|
1093
1214
|
}
|
|
1094
|
-
function
|
|
1095
|
-
const
|
|
1096
|
-
const
|
|
1097
|
-
if (
|
|
1098
|
-
|
|
1099
|
-
if (onConnection !== null) return onConnection;
|
|
1100
|
-
if (meta) {
|
|
1101
|
-
for (const key of Object.keys(meta)) {
|
|
1102
|
-
if (key.startsWith("claudecode/")) return "claude-code";
|
|
1103
|
-
}
|
|
1104
|
-
}
|
|
1105
|
-
return null;
|
|
1215
|
+
function observeClient(extra, options) {
|
|
1216
|
+
const info = orNothing(() => observedInfo(extra, options));
|
|
1217
|
+
const headers = orNothing(() => observedHeaders(extra, options));
|
|
1218
|
+
if (info === void 0 && headers === void 0) return null;
|
|
1219
|
+
return { ...info && { info }, ...headers && { headers } };
|
|
1106
1220
|
}
|
|
1107
1221
|
|
|
1108
1222
|
// src/integrations/mcp/sessionResolution.ts
|
|
@@ -1132,11 +1246,6 @@ function registerAnnotationTool(server, options) {
|
|
|
1132
1246
|
},
|
|
1133
1247
|
async (args, extra) => {
|
|
1134
1248
|
const meta = extraMeta(extra);
|
|
1135
|
-
const runtime = detectAgentRuntime(meta, {
|
|
1136
|
-
envelope: extraEnvelope(extra),
|
|
1137
|
-
server,
|
|
1138
|
-
scrubber: options.scrubber
|
|
1139
|
-
}) ?? UNKNOWN_AGENT_RUNTIME;
|
|
1140
1249
|
const scrubbedMeta = scrubOrNull(
|
|
1141
1250
|
options.scrubber,
|
|
1142
1251
|
meta ? roundMetaCoordinates(meta) : null,
|
|
@@ -1159,9 +1268,9 @@ function registerAnnotationTool(server, options) {
|
|
|
1159
1268
|
sequence_number: options.counter.next(sessionId),
|
|
1160
1269
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1161
1270
|
consent_token: options.consentToken,
|
|
1162
|
-
agent_runtime: runtime,
|
|
1163
1271
|
principal,
|
|
1164
1272
|
transport_observed: observeTransport(extra),
|
|
1273
|
+
client_observed: observeClient(extra, { server, scrubber: options.scrubber }),
|
|
1165
1274
|
runtime_meta: scrubbedMeta,
|
|
1166
1275
|
payload: {
|
|
1167
1276
|
// Agent-facing names -> wire keys, as with `overall_task`
|
|
@@ -1468,7 +1577,7 @@ function resolveTenantId(explicit, vendorId) {
|
|
|
1468
1577
|
if (fromEnv) return fromEnv;
|
|
1469
1578
|
return vendorId;
|
|
1470
1579
|
}
|
|
1471
|
-
function resolveBatonConfig(config
|
|
1580
|
+
function resolveBatonConfig(config) {
|
|
1472
1581
|
const dsnString = selectDsn(
|
|
1473
1582
|
config.dsn,
|
|
1474
1583
|
{
|
|
@@ -1504,14 +1613,7 @@ function resolveBatonConfig(config, serverName) {
|
|
|
1504
1613
|
// in the server instructions and the annotation tool description, which is
|
|
1505
1614
|
// the whitelabel obligation the validator cites when it refuses the empty
|
|
1506
1615
|
// one. ⚠ Python uses `or` here and still has the inconsistency.
|
|
1507
|
-
|
|
1508
|
-
// Between the explicit value and the segment sits the server's own name,
|
|
1509
|
-
// under the guards in `annotationName.ts`: the segment is an opaque
|
|
1510
|
-
// `srv-<8 hex>`, and it was what agents were told the vendor is called.
|
|
1511
|
-
vendorDisplayName: config.vendorDisplayName ?? displayNameFromServer(serverName, {
|
|
1512
|
-
vendorId: dsn.vendorId,
|
|
1513
|
-
annotationToolName: config.annotationToolName
|
|
1514
|
-
}) ?? dsn.vendorId,
|
|
1616
|
+
vendorDisplayName: config.vendorDisplayName ?? dsn.vendorId,
|
|
1515
1617
|
// `??`, not `||`: an explicit empty string must survive to the validation
|
|
1516
1618
|
// below and be REFUSED there, rather than be quietly replaced by the
|
|
1517
1619
|
// default it was deliberately not left as.
|
|
@@ -1525,6 +1627,14 @@ function resolveBatonConfig(config, serverName) {
|
|
|
1525
1627
|
sink: new HttpSink(dsn.origin, { apiKey: dsn.key })
|
|
1526
1628
|
};
|
|
1527
1629
|
}
|
|
1630
|
+
function withServerDisplayName(resolved, supplied, serverName) {
|
|
1631
|
+
if (supplied.vendorDisplayName !== void 0) return resolved;
|
|
1632
|
+
const fromServer = displayNameFromServer(serverName, {
|
|
1633
|
+
vendorId: resolved.vendorId,
|
|
1634
|
+
annotationToolName: supplied.annotationToolName
|
|
1635
|
+
});
|
|
1636
|
+
return fromServer === void 0 ? resolved : { ...resolved, vendorDisplayName: fromServer };
|
|
1637
|
+
}
|
|
1528
1638
|
var RENAMED_KEYS = {
|
|
1529
1639
|
resolveUser: "resolvePrincipal"
|
|
1530
1640
|
};
|
|
@@ -1761,13 +1871,8 @@ function injectGoalParams(inputSchema, intentParamMode) {
|
|
|
1761
1871
|
function buildIntentFields(existingShape, zodImpl, intentParamMode) {
|
|
1762
1872
|
const dispositions = {};
|
|
1763
1873
|
const toInject = {};
|
|
1764
|
-
if (USER_GOAL_PARAM_NAME in existingShape) {
|
|
1765
|
-
dispositions[USER_GOAL_PARAM_NAME] = "native";
|
|
1766
|
-
} else {
|
|
1767
|
-
dispositions[USER_GOAL_PARAM_NAME] = "injected";
|
|
1768
|
-
toInject[USER_GOAL_PARAM_NAME] = zodImpl.string().optional().describe(buildUserGoalParamDescription({ intentParamMode }));
|
|
1769
|
-
}
|
|
1770
1874
|
for (const [name, buildDescription] of [
|
|
1875
|
+
[USER_GOAL_PARAM_NAME, buildUserGoalParamDescription],
|
|
1771
1876
|
[EXPECTED_RESULT_PARAM_NAME, buildExpectedResultParamDescription],
|
|
1772
1877
|
[OVERALL_TASK_PARAM_NAME, buildOverallTaskParamDescription]
|
|
1773
1878
|
]) {
|
|
@@ -1775,7 +1880,7 @@ function buildIntentFields(existingShape, zodImpl, intentParamMode) {
|
|
|
1775
1880
|
dispositions[name] = "native";
|
|
1776
1881
|
} else {
|
|
1777
1882
|
dispositions[name] = "injected";
|
|
1778
|
-
toInject[name] = zodImpl.string().optional().describe(buildDescription());
|
|
1883
|
+
toInject[name] = zodImpl.string().optional().describe(buildDescription({ intentParamMode }));
|
|
1779
1884
|
}
|
|
1780
1885
|
}
|
|
1781
1886
|
return { dispositions, toInject };
|
|
@@ -1791,21 +1896,23 @@ function injectGoalParamsV2(inputSchema, intentParamMode) {
|
|
|
1791
1896
|
return { schema: inputSchema, dispositions };
|
|
1792
1897
|
return { schema: candidate.extend(toInject), dispositions };
|
|
1793
1898
|
}
|
|
1794
|
-
function
|
|
1899
|
+
function advertiseIntentParamsRequired(result, names, isInjected) {
|
|
1795
1900
|
const tools = result?.tools;
|
|
1796
1901
|
if (!Array.isArray(tools)) return result;
|
|
1797
1902
|
let changed = false;
|
|
1798
1903
|
const advertised = tools.map((tool) => {
|
|
1799
1904
|
const entry = tool;
|
|
1800
|
-
|
|
1801
|
-
|
|
1905
|
+
const toolName = entry?.name;
|
|
1906
|
+
if (typeof toolName !== "string") return tool;
|
|
1907
|
+
const schema = entry?.inputSchema;
|
|
1802
1908
|
if (!schema || typeof schema !== "object") return tool;
|
|
1803
|
-
const
|
|
1804
|
-
if (!Array.isArray(
|
|
1805
|
-
const required =
|
|
1806
|
-
|
|
1909
|
+
const declared2 = schema.required ?? [];
|
|
1910
|
+
if (!Array.isArray(declared2)) return tool;
|
|
1911
|
+
const required = declared2;
|
|
1912
|
+
const missing = names.filter((name) => isInjected(toolName, name) && !required.includes(name));
|
|
1913
|
+
if (missing.length === 0) return tool;
|
|
1807
1914
|
changed = true;
|
|
1808
|
-
return { ...entry, inputSchema: { ...schema, required: [...required,
|
|
1915
|
+
return { ...entry, inputSchema: { ...schema, required: [...required, ...missing] } };
|
|
1809
1916
|
});
|
|
1810
1917
|
return changed ? { ...result, tools: advertised } : result;
|
|
1811
1918
|
}
|
|
@@ -1819,10 +1926,10 @@ function toolInputJsonSchema(inputSchema) {
|
|
|
1819
1926
|
}
|
|
1820
1927
|
|
|
1821
1928
|
// src/integrations/mcp/surface.ts
|
|
1822
|
-
var
|
|
1929
|
+
var import_node_crypto2 = require("crypto");
|
|
1823
1930
|
function surfaceHash(surface) {
|
|
1824
1931
|
const canonical = canonicalJson(surface);
|
|
1825
|
-
const digest = (0,
|
|
1932
|
+
const digest = (0, import_node_crypto2.createHash)("sha256").update(canonical, "utf-8").digest("hex");
|
|
1826
1933
|
return `sha256:${digest}`;
|
|
1827
1934
|
}
|
|
1828
1935
|
function canonicalJson(value) {
|
|
@@ -1854,7 +1961,11 @@ function assembleSurface(serverMeta, tools) {
|
|
|
1854
1961
|
function buildSeamAugmentations(options) {
|
|
1855
1962
|
return {
|
|
1856
1963
|
injected_tools: [...options.injectedToolNames].sort(),
|
|
1857
|
-
intent_param: options.intentParamMode !== "off" ? {
|
|
1964
|
+
intent_param: options.intentParamMode !== "off" ? {
|
|
1965
|
+
names: [...options.intentParamNames].sort(),
|
|
1966
|
+
required_names: [...options.requiredParamNames].sort(),
|
|
1967
|
+
mode: options.intentParamMode
|
|
1968
|
+
} : null,
|
|
1858
1969
|
instructions_suffix: true
|
|
1859
1970
|
};
|
|
1860
1971
|
}
|
|
@@ -1865,16 +1976,17 @@ var wired = /* @__PURE__ */ new WeakSet();
|
|
|
1865
1976
|
var CLEAN_OUTCOME = { failed: false, raised: false, result: void 0 };
|
|
1866
1977
|
var callSlots = new import_node_async_hooks.AsyncLocalStorage();
|
|
1867
1978
|
var SurfaceState = class {
|
|
1868
|
-
constructor(serverMeta) {
|
|
1979
|
+
constructor(serverMeta, emittedHashes) {
|
|
1869
1980
|
this.serverMeta = serverMeta;
|
|
1981
|
+
this.emittedHashes = emittedHashes;
|
|
1870
1982
|
}
|
|
1871
1983
|
serverMeta;
|
|
1984
|
+
emittedHashes;
|
|
1872
1985
|
/** Every tool this install has seen, keyed by its CURRENT name. `disabled`
|
|
1873
1986
|
* is tracked alongside rather than pruned, so re-enabling costs nothing and
|
|
1874
1987
|
* — crucially — never needs a re-capture, which would re-read Baton's own
|
|
1875
1988
|
* injected schema as if it were vendor-true. */
|
|
1876
1989
|
rawTools = /* @__PURE__ */ new Map();
|
|
1877
|
-
emittedHashes = /* @__PURE__ */ new Set();
|
|
1878
1990
|
dirty = false;
|
|
1879
1991
|
/** `inputSchemaJson` is already converted — by `ctx.vendorToolJsonSchema`,
|
|
1880
1992
|
* which picks the spelling THIS SDK major puts on the wire. */
|
|
@@ -1926,23 +2038,13 @@ function messageOf(err) {
|
|
|
1926
2038
|
}
|
|
1927
2039
|
async function resolveEnvelopeParts(ctx, extra) {
|
|
1928
2040
|
const meta = extraMeta(extra);
|
|
1929
|
-
const runtime = detectAgentRuntime(meta, {
|
|
1930
|
-
// v2 lifts the reserved `io.modelcontextprotocol/*` keys out of
|
|
1931
|
-
// `_meta`; 1.x leaves them in. Both are handed over — see
|
|
1932
|
-
// `mcpTypes.extraEnvelope`.
|
|
1933
|
-
envelope: extraEnvelope(extra),
|
|
1934
|
-
// Tier 2's carrier: neither peer puts client identity on the
|
|
1935
|
-
// handler context, both expose the cached handshake on the server.
|
|
1936
|
-
server: ctx.server,
|
|
1937
|
-
scrubber: ctx.scrubber
|
|
1938
|
-
}) ?? UNKNOWN_AGENT_RUNTIME;
|
|
1939
2041
|
const scrubbedMeta = scrubOrNull(ctx.scrubber, meta ? roundMetaCoordinates(meta) : null, "_meta");
|
|
1940
2042
|
const sessionId = await resolveSessionId(ctx.fallbackSessionId, extra);
|
|
1941
|
-
return { sessionId,
|
|
2043
|
+
return { sessionId, scrubbedMeta };
|
|
1942
2044
|
}
|
|
1943
2045
|
async function openCall(ctx, toolName, extra, params, registered = true) {
|
|
1944
2046
|
await maybeEmitSurfaceSnapshot(ctx);
|
|
1945
|
-
const { sessionId,
|
|
2047
|
+
const { sessionId, scrubbedMeta } = await resolveEnvelopeParts(ctx, extra);
|
|
1946
2048
|
const dispositions = ctx.paramRegistry.get(toolName);
|
|
1947
2049
|
const rawIntent = extractGoalParam(params, USER_GOAL_PARAM_NAME, toolName, dispositions, registered);
|
|
1948
2050
|
const rawExpected = extractGoalParam(
|
|
@@ -1956,7 +2058,7 @@ async function openCall(ctx, toolName, extra, params, registered = true) {
|
|
|
1956
2058
|
const scrubbedIntent = scrubOrNull(ctx.scrubber, rawIntent, USER_GOAL_PARAM_NAME);
|
|
1957
2059
|
const scrubbedExpected = scrubOrNull(ctx.scrubber, rawExpected, EXPECTED_RESULT_PARAM_NAME);
|
|
1958
2060
|
const scrubbedWorkflow = scrubOrNull(ctx.scrubber, rawWorkflow, OVERALL_TASK_PARAM_NAME);
|
|
1959
|
-
const callId = (
|
|
2061
|
+
const callId = uuid7();
|
|
1960
2062
|
const principal = await resolveCallPrincipal(
|
|
1961
2063
|
ctx.resolvePrincipal,
|
|
1962
2064
|
{ extra, toolName, arguments: params }
|
|
@@ -1966,19 +2068,12 @@ async function openCall(ctx, toolName, extra, params, registered = true) {
|
|
|
1966
2068
|
vendor_id: ctx.vendorId,
|
|
1967
2069
|
session_id: sessionId,
|
|
1968
2070
|
consent_token: ctx.consentToken,
|
|
1969
|
-
|
|
1970
|
-
//
|
|
1971
|
-
//
|
|
1972
|
-
// annotation, the same five Python stamps (`middleware.py`
|
|
1973
|
-
// 503/547/586/644/674; it was four until the returned shape landed
|
|
1974
|
-
// there too). `surface_snapshot` is deliberately NOT among them: it
|
|
1975
|
-
// describes the SERVER and is captured outside any call, so there is no
|
|
1976
|
-
// caller to name (register D5).
|
|
2071
|
+
// Every `...common` site is an event with a caller. `surface_snapshot`
|
|
2072
|
+
// is deliberately not among them: it describes the SERVER and is captured
|
|
2073
|
+
// outside any call, so it has no caller, transport or client to name.
|
|
1977
2074
|
principal,
|
|
1978
|
-
// Same five stamps, same exclusion: a surface_snapshot describes the
|
|
1979
|
-
// SERVER and is captured outside any call, so it has no caller's
|
|
1980
|
-
// transport to name any more than it has a caller to name.
|
|
1981
2075
|
transport_observed: observeTransport(extra),
|
|
2076
|
+
client_observed: observeClient(extra, ctx),
|
|
1982
2077
|
runtime_meta: scrubbedMeta
|
|
1983
2078
|
};
|
|
1984
2079
|
if (scrubbedIntent !== null && ctx.tracker.claim(sessionId)) {
|
|
@@ -2200,6 +2295,7 @@ function extractGoalParam(args, paramName, toolName, dispositions, registered) {
|
|
|
2200
2295
|
delete args[paramName];
|
|
2201
2296
|
return typeof raw === "string" && raw.trim() ? raw : null;
|
|
2202
2297
|
}
|
|
2298
|
+
var MAX_REMEMBERED_SURFACES = 1024;
|
|
2203
2299
|
async function maybeEmitSurfaceSnapshot(ctx) {
|
|
2204
2300
|
if (!ctx.surfaceState.dirty) return;
|
|
2205
2301
|
ctx.surfaceState.dirty = false;
|
|
@@ -2213,21 +2309,24 @@ async function maybeEmitSurfaceSnapshot(ctx) {
|
|
|
2213
2309
|
`);
|
|
2214
2310
|
return;
|
|
2215
2311
|
}
|
|
2216
|
-
|
|
2312
|
+
const emitted = ctx.surfaceState.emittedHashes;
|
|
2313
|
+
if (emitted.has(digest)) return;
|
|
2314
|
+
if (emitted.size >= MAX_REMEMBERED_SURFACES) emitted.clear();
|
|
2315
|
+
emitted.add(digest);
|
|
2217
2316
|
const seam = buildSeamAugmentations({
|
|
2218
2317
|
injectedToolNames: [ctx.annotationToolName],
|
|
2219
2318
|
intentParamNames: [USER_GOAL_PARAM_NAME, EXPECTED_RESULT_PARAM_NAME, OVERALL_TASK_PARAM_NAME],
|
|
2220
|
-
intentParamMode: ctx.intentParamMode
|
|
2319
|
+
intentParamMode: ctx.intentParamMode,
|
|
2320
|
+
requiredParamNames: requiredParamNames(ctx.intentParamMode)
|
|
2221
2321
|
});
|
|
2222
2322
|
try {
|
|
2223
2323
|
await ctx.emitSurface(ctx.fallbackSessionId, digest, { ...snapshot, seam_augmentations: seam });
|
|
2224
2324
|
} catch (err) {
|
|
2225
2325
|
process.stderr.write(`baton: surface snapshot capture failed: ${String(err)}
|
|
2226
2326
|
`);
|
|
2327
|
+
emitted.delete(digest);
|
|
2227
2328
|
ctx.surfaceState.dirty = true;
|
|
2228
|
-
return;
|
|
2229
2329
|
}
|
|
2230
|
-
ctx.surfaceState.emittedHashes.add(digest);
|
|
2231
2330
|
}
|
|
2232
2331
|
function captureAndInject(name, entry, ctx) {
|
|
2233
2332
|
if (!entry || typeof entry !== "object") return;
|
|
@@ -2241,6 +2340,7 @@ function captureAndInject(name, entry, ctx) {
|
|
|
2241
2340
|
mutable.inputSchema = schema;
|
|
2242
2341
|
ctx.bustSchemaMemo(name);
|
|
2243
2342
|
ctx.paramRegistry.set(name, dispositions);
|
|
2343
|
+
if (dispositions[OVERALL_TASK_PARAM_NAME] === "native") ctx.dropSubagentClause(name);
|
|
2244
2344
|
} else {
|
|
2245
2345
|
ctx.paramRegistry.delete(name);
|
|
2246
2346
|
}
|
|
@@ -2261,6 +2361,7 @@ function installRequestSeam(lowLevel, method, seamTag, wrap) {
|
|
|
2261
2361
|
const handler = handlers.get(method);
|
|
2262
2362
|
if (typeof handler !== "function" || handler[seamTag]) return;
|
|
2263
2363
|
const seamed = wrap(handler);
|
|
2364
|
+
for (const tag of Object.getOwnPropertySymbols(handler)) seamed[tag] = handler[tag];
|
|
2264
2365
|
seamed[seamTag] = true;
|
|
2265
2366
|
handlers.set(method, seamed);
|
|
2266
2367
|
};
|
|
@@ -2272,10 +2373,11 @@ function installRequestSeam(lowLevel, method, seamTag, wrap) {
|
|
|
2272
2373
|
};
|
|
2273
2374
|
}
|
|
2274
2375
|
function installToolsListSeam(lowLevel, ctx) {
|
|
2275
|
-
const
|
|
2376
|
+
const names = requiredParamNames(ctx.intentParamMode);
|
|
2377
|
+
const isInjected = (toolName, paramName) => ctx.paramRegistry.get(toolName)?.[paramName] === "injected";
|
|
2276
2378
|
const advertise = (result) => {
|
|
2277
2379
|
try {
|
|
2278
|
-
return
|
|
2380
|
+
return advertiseIntentParamsRequired(result, names, isInjected);
|
|
2279
2381
|
} catch (err) {
|
|
2280
2382
|
process.stderr.write(
|
|
2281
2383
|
`baton: tools/list advertisement failed; serving the SDK's own result: ${String(err)}
|
|
@@ -2293,7 +2395,8 @@ var LIFECYCLE_SEAMS = {
|
|
|
2293
2395
|
resourceList: /* @__PURE__ */ Symbol("batonResourceListSeam"),
|
|
2294
2396
|
resourceRead: /* @__PURE__ */ Symbol("batonResourceReadSeam"),
|
|
2295
2397
|
promptList: /* @__PURE__ */ Symbol("batonPromptListSeam"),
|
|
2296
|
-
promptGet: /* @__PURE__ */ Symbol("batonPromptGetSeam")
|
|
2398
|
+
promptGet: /* @__PURE__ */ Symbol("batonPromptGetSeam"),
|
|
2399
|
+
toolList: /* @__PURE__ */ Symbol("batonToolListSeam")
|
|
2297
2400
|
};
|
|
2298
2401
|
function installLifecycleSeam(lowLevel, ctx, spec) {
|
|
2299
2402
|
installRequestSeam(lowLevel, spec.method, spec.seamTag, (handler) => async (...args) => {
|
|
@@ -2340,6 +2443,7 @@ function installLifecycleSeam(lowLevel, ctx, spec) {
|
|
|
2340
2443
|
async function openLifecycleCall(ctx, spec, requestParams, extra) {
|
|
2341
2444
|
try {
|
|
2342
2445
|
const parts = await resolveEnvelopeParts(ctx, extra);
|
|
2446
|
+
const principal = spec.resolvesPrincipal ? await resolveCallPrincipal(ctx.resolvePrincipal, { extra, toolName: null, arguments: {} }) : null;
|
|
2343
2447
|
return {
|
|
2344
2448
|
sessionId: parts.sessionId,
|
|
2345
2449
|
subject: spec.subjectKey === void 0 ? {} : { [spec.subjectKey]: stringSubject(requestParams[spec.subjectKey]) },
|
|
@@ -2348,8 +2452,9 @@ async function openLifecycleCall(ctx, spec, requestParams, extra) {
|
|
|
2348
2452
|
vendor_id: ctx.vendorId,
|
|
2349
2453
|
session_id: parts.sessionId,
|
|
2350
2454
|
consent_token: ctx.consentToken,
|
|
2351
|
-
|
|
2455
|
+
principal,
|
|
2352
2456
|
transport_observed: observeTransport(extra),
|
|
2457
|
+
client_observed: observeClient(extra, ctx),
|
|
2353
2458
|
runtime_meta: parts.scrubbedMeta
|
|
2354
2459
|
}
|
|
2355
2460
|
};
|
|
@@ -2422,6 +2527,18 @@ var LIFECYCLE_SPECS = [
|
|
|
2422
2527
|
startParams: (params) => ({
|
|
2423
2528
|
params: paramsBag(params["arguments"])
|
|
2424
2529
|
})
|
|
2530
|
+
},
|
|
2531
|
+
{
|
|
2532
|
+
method: TOOLS_LIST_METHOD,
|
|
2533
|
+
seamTag: LIFECYCLE_SEAMS.toolList,
|
|
2534
|
+
schemas: {
|
|
2535
|
+
start: ToolListStartEventSchema,
|
|
2536
|
+
end: ToolListEndEventSchema,
|
|
2537
|
+
error: ToolListErrorEventSchema
|
|
2538
|
+
},
|
|
2539
|
+
countKey: "tools",
|
|
2540
|
+
startParams: NO_PARAMS,
|
|
2541
|
+
resolvesPrincipal: true
|
|
2425
2542
|
}
|
|
2426
2543
|
];
|
|
2427
2544
|
function installLifecycleSeams(lowLevel, ctx) {
|
|
@@ -2552,9 +2669,6 @@ async function emitRequestSideFailure(outcome, args, toolEntry, ctx) {
|
|
|
2552
2669
|
})
|
|
2553
2670
|
);
|
|
2554
2671
|
}
|
|
2555
|
-
function isThenable(value) {
|
|
2556
|
-
return typeof value?.then === "function";
|
|
2557
|
-
}
|
|
2558
2672
|
function wrapIfNeeded(nameRef, entry, ctx) {
|
|
2559
2673
|
if (!entry || typeof entry !== "object") return;
|
|
2560
2674
|
const mutable = entry;
|
|
@@ -2630,47 +2744,57 @@ function withBaton(server, supplied = {}) {
|
|
|
2630
2744
|
annotationToolName: ""
|
|
2631
2745
|
});
|
|
2632
2746
|
}
|
|
2747
|
+
return install(server, supplied, resolveBatonConfig(supplied), /* @__PURE__ */ new Set());
|
|
2748
|
+
}
|
|
2749
|
+
function createBaton(supplied = {}) {
|
|
2750
|
+
const disabledBy = captureDisabled();
|
|
2751
|
+
if (disabledBy !== null) {
|
|
2752
|
+
logDisabled(disabledBy, "createBaton");
|
|
2753
|
+
const unused = supplied.sink ?? new DisabledSink();
|
|
2754
|
+
return { wrap: () => {
|
|
2755
|
+
}, flush: () => unused.flush(), aclose: () => unused.aclose() };
|
|
2756
|
+
}
|
|
2757
|
+
const resolved = resolveBatonConfig(supplied);
|
|
2758
|
+
deriveAnnotationToolName(resolved.vendorId, supplied.annotationToolName);
|
|
2759
|
+
const sink = resolved.sink ?? new StdoutSink();
|
|
2760
|
+
const config = {
|
|
2761
|
+
...resolved,
|
|
2762
|
+
sink,
|
|
2763
|
+
tenantId: resolveTenantId(resolved.tenantId, resolved.vendorId)
|
|
2764
|
+
};
|
|
2765
|
+
const emittedSurfaceHashes = /* @__PURE__ */ new Set();
|
|
2766
|
+
return {
|
|
2767
|
+
wrap: (server) => {
|
|
2768
|
+
install(server, supplied, config, emittedSurfaceHashes);
|
|
2769
|
+
},
|
|
2770
|
+
flush: () => sink.flush(),
|
|
2771
|
+
aclose: () => sink.aclose()
|
|
2772
|
+
};
|
|
2773
|
+
}
|
|
2774
|
+
function install(server, supplied, resolved, emittedSurfaceHashes) {
|
|
2633
2775
|
const internals = server;
|
|
2634
2776
|
const serverName = usableServerName(() => ({
|
|
2635
2777
|
name: internals.server._serverInfo?.name,
|
|
2636
2778
|
className: internals.constructor.name
|
|
2637
2779
|
}));
|
|
2638
|
-
const config =
|
|
2780
|
+
const config = withServerDisplayName(resolved, supplied, serverName);
|
|
2639
2781
|
const sink = config.sink ?? new StdoutSink();
|
|
2640
2782
|
const annotationToolName = resolveAnnotationToolName(serverName, config);
|
|
2641
2783
|
const intentParamMode = config.intentParamMode ?? "required";
|
|
2642
|
-
const
|
|
2643
|
-
const fallbackSessionId = `sdk-${(
|
|
2784
|
+
const counter2 = new SessionCounter();
|
|
2785
|
+
const fallbackSessionId = `sdk-${uuid7()}`;
|
|
2644
2786
|
const tenantId = resolveTenantId(config.tenantId, config.vendorId);
|
|
2645
2787
|
const scrubber = config.scrubber ?? new Scrubber().scrub;
|
|
2646
2788
|
const serverMeta = buildServerMeta(server.server);
|
|
2647
|
-
const surfaceState = new SurfaceState(serverMeta);
|
|
2789
|
+
const surfaceState = new SurfaceState(serverMeta, emittedSurfaceHashes);
|
|
2648
2790
|
const emitSurface = async (sessionId, digest, snapshot) => {
|
|
2649
2791
|
const event = SurfaceSnapshotEventSchema.parse({
|
|
2650
2792
|
tenant_id: tenantId,
|
|
2651
2793
|
vendor_id: config.vendorId,
|
|
2652
2794
|
session_id: sessionId,
|
|
2653
2795
|
consent_token: config.consentToken,
|
|
2654
|
-
sequence_number:
|
|
2796
|
+
sequence_number: counter2.next(sessionId),
|
|
2655
2797
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2656
|
-
// Deliberately the literal, NOT the ladder — and an earlier comment
|
|
2657
|
-
// here justified that by saying no call is in scope, which is false:
|
|
2658
|
-
// `maybeEmitSurfaceSnapshot` is the first line of `batonWrap`, so the
|
|
2659
|
-
// handshake has happened and tier 2's carrier is live in this closure.
|
|
2660
|
-
// The real reason is that the snapshot describes the VENDOR'S SURFACE,
|
|
2661
|
-
// which is the same whoever is calling. It is hashed and emitted at
|
|
2662
|
-
// most once per process per surface, so the client that happens to
|
|
2663
|
-
// trigger it is whichever one called first — attributing the surface to
|
|
2664
|
-
// that client would read as a fact about the surface and be an accident
|
|
2665
|
-
// of timing. Python hardcodes it from the same in-call position
|
|
2666
|
-
// (`_tool_wrap.py`), so this is parity, not a gap.
|
|
2667
|
-
//
|
|
2668
|
-
// ⚠ Consequence worth knowing: one session emits `surface_snapshot`
|
|
2669
|
-
// with `unknown` and everything else with the client's name, so a
|
|
2670
|
-
// consumer grouping on `agent_runtime` alone sees two runtimes for one
|
|
2671
|
-
// client. Intended; group surfaces on `(tenant_id, vendor_id,
|
|
2672
|
-
// surface_hash)`, which is what the Console's table is keyed on.
|
|
2673
|
-
agent_runtime: UNKNOWN_AGENT_RUNTIME,
|
|
2674
2798
|
payload: {
|
|
2675
2799
|
surface_hash: digest,
|
|
2676
2800
|
server_info: snapshot.server_info,
|
|
@@ -2686,6 +2810,15 @@ function withBaton(server, supplied = {}) {
|
|
|
2686
2810
|
const memo = internals._toolInputSchemaJson;
|
|
2687
2811
|
if (memo && typeof memo === "object") delete memo[name];
|
|
2688
2812
|
};
|
|
2813
|
+
const dropSubagentClause = (name) => {
|
|
2814
|
+
const current = internals.server._instructions;
|
|
2815
|
+
if (typeof current !== "string" || !current.includes(INSTRUCTIONS_SUBAGENT_CLAUSE)) return;
|
|
2816
|
+
internals.server._instructions = current.replace(INSTRUCTIONS_SUBAGENT_CLAUSE, "");
|
|
2817
|
+
process.stderr.write(
|
|
2818
|
+
`baton: tool ${JSON.stringify(name)} declares its own overall_task, so the subagent sentence was removed from the server instructions; a session that already started keeps it
|
|
2819
|
+
`
|
|
2820
|
+
);
|
|
2821
|
+
};
|
|
2689
2822
|
const isV2Server = typeof internals.toolInputSchemaJson === "function";
|
|
2690
2823
|
const vendorToolJsonSchema = (name, inputSchema) => {
|
|
2691
2824
|
if (isV2Server) {
|
|
@@ -2710,7 +2843,7 @@ function withBaton(server, supplied = {}) {
|
|
|
2710
2843
|
const tracker = new ProactiveTracker();
|
|
2711
2844
|
const ctx = {
|
|
2712
2845
|
sink,
|
|
2713
|
-
counter,
|
|
2846
|
+
counter: counter2,
|
|
2714
2847
|
tenantId,
|
|
2715
2848
|
vendorId: config.vendorId,
|
|
2716
2849
|
consentToken: config.consentToken,
|
|
@@ -2724,13 +2857,15 @@ function withBaton(server, supplied = {}) {
|
|
|
2724
2857
|
resolvePrincipal: config.resolvePrincipal,
|
|
2725
2858
|
vendorToolJsonSchema,
|
|
2726
2859
|
bustSchemaMemo,
|
|
2860
|
+
dropSubagentClause,
|
|
2727
2861
|
tracker,
|
|
2728
2862
|
surfaceState,
|
|
2729
2863
|
emitSurface
|
|
2730
2864
|
};
|
|
2731
2865
|
const instructions = buildServerInstructions({
|
|
2732
2866
|
vendorDisplayName: config.vendorDisplayName,
|
|
2733
|
-
annotationToolName
|
|
2867
|
+
annotationToolName,
|
|
2868
|
+
intentParamMode
|
|
2734
2869
|
});
|
|
2735
2870
|
internals.server._instructions = instructions;
|
|
2736
2871
|
if (intentParamMode === "required") installToolsListSeam(internals.server, ctx);
|
|
@@ -2865,6 +3000,13 @@ function principalFromOAuthEmail(context) {
|
|
|
2865
3000
|
ToolCallErrorPayloadSchema,
|
|
2866
3001
|
ToolCallStartEventSchema,
|
|
2867
3002
|
ToolCallStartPayloadSchema,
|
|
3003
|
+
ToolListEndEventSchema,
|
|
3004
|
+
ToolListEndPayloadSchema,
|
|
3005
|
+
ToolListErrorEventSchema,
|
|
3006
|
+
ToolListErrorPayloadSchema,
|
|
3007
|
+
ToolListStartEventSchema,
|
|
3008
|
+
ToolListStartPayloadSchema,
|
|
3009
|
+
createBaton,
|
|
2868
3010
|
identityScrub,
|
|
2869
3011
|
principalFromOAuthEmail,
|
|
2870
3012
|
principalFromOAuthSub,
|