mcp-from-openapi 2.6.1 → 2.7.0
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 +10 -3
- package/annotations.d.ts +16 -1
- package/arazzo-expressions.d.ts +19 -0
- package/arazzo-types.d.ts +262 -0
- package/arazzo.d.ts +45 -0
- package/elicitation.d.ts +44 -0
- package/errors.d.ts +8 -0
- package/esm/index.mjs +1804 -33
- package/esm/package.json +3 -1
- package/generator.d.ts +14 -0
- package/index.d.ts +12 -2
- package/index.js +1812 -33
- package/naming-presets.d.ts +49 -0
- package/package.json +3 -1
- package/type-signature.d.ts +43 -0
- package/types.d.ts +91 -4
- package/validator.d.ts +5 -0
package/index.js
CHANGED
|
@@ -30,8 +30,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
+
ArazzoError: () => ArazzoError,
|
|
33
34
|
BLOCKED_HOSTNAMES: () => BLOCKED_HOSTNAMES,
|
|
34
35
|
BUILTIN_FORMAT_RESOLVERS: () => BUILTIN_FORMAT_RESOLVERS,
|
|
36
|
+
CODECALL_RESERVED_NAMESPACES: () => CODECALL_RESERVED_NAMESPACES,
|
|
35
37
|
GenerationError: () => GenerationError,
|
|
36
38
|
LoadError: () => LoadError,
|
|
37
39
|
OpenAPIToolError: () => OpenAPIToolError,
|
|
@@ -58,10 +60,14 @@ __export(index_exports, {
|
|
|
58
60
|
decodeIpv4MappedIpv6: () => decodeIpv4MappedIpv6,
|
|
59
61
|
defaultLookup: () => defaultLookup,
|
|
60
62
|
demoteFormats: () => demoteFormats,
|
|
63
|
+
deriveSecurityElicitations: () => deriveSecurityElicitations,
|
|
64
|
+
dottedNaming: () => dottedNaming,
|
|
65
|
+
emitToolTypeScript: () => emitToolTypeScript,
|
|
61
66
|
enforceClosedObjects: () => enforceClosedObjects,
|
|
62
67
|
ensureArrayItems: () => ensureArrayItems,
|
|
63
68
|
estimateToolTokens: () => estimateToolTokens,
|
|
64
69
|
extractExtensionOverrides: () => extractExtensionOverrides,
|
|
70
|
+
fromArazzo: () => fromArazzo,
|
|
65
71
|
inferAnnotationsFromMethod: () => inferAnnotationsFromMethod,
|
|
66
72
|
inlineLocalRefs: () => inlineLocalRefs,
|
|
67
73
|
isBlockedAddress: () => isBlockedAddress,
|
|
@@ -69,11 +75,13 @@ __export(index_exports, {
|
|
|
69
75
|
isReferenceObject: () => isReferenceObject,
|
|
70
76
|
lintDocument: () => lintDocument,
|
|
71
77
|
normalizeSsrfOptions: () => normalizeSsrfOptions,
|
|
78
|
+
parseRuntimeExpression: () => parseRuntimeExpression,
|
|
72
79
|
requireAllProperties: () => requireAllProperties,
|
|
73
80
|
resolveExtensionEnabled: () => resolveExtensionEnabled,
|
|
74
81
|
resolveSchemaFormats: () => resolveSchemaFormats,
|
|
75
82
|
safeFetch: () => safeFetch,
|
|
76
83
|
toJsonSchema: () => toJsonSchema,
|
|
84
|
+
toPascalIdentifier: () => toPascalIdentifier,
|
|
77
85
|
toSdkTool: () => toSdkTool
|
|
78
86
|
});
|
|
79
87
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -86,9 +94,24 @@ function isReferenceObject(obj) {
|
|
|
86
94
|
return obj && typeof obj === "object" && "$ref" in obj;
|
|
87
95
|
}
|
|
88
96
|
function toJsonSchema(schema) {
|
|
97
|
+
return convertSchema(schema, /* @__PURE__ */ new Set());
|
|
98
|
+
}
|
|
99
|
+
function convertSchema(schema, stack) {
|
|
89
100
|
if (isReferenceObject(schema)) {
|
|
90
101
|
return { $ref: schema.$ref };
|
|
91
102
|
}
|
|
103
|
+
if (stack.has(schema)) {
|
|
104
|
+
return {};
|
|
105
|
+
}
|
|
106
|
+
stack.add(schema);
|
|
107
|
+
try {
|
|
108
|
+
return convertSchemaInner(schema, stack);
|
|
109
|
+
} finally {
|
|
110
|
+
stack.delete(schema);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function convertSchemaInner(schema, stack) {
|
|
114
|
+
const recurse = (value) => convertSchema(value, stack);
|
|
92
115
|
const { exclusiveMaximum, exclusiveMinimum, maximum, minimum, ...rest } = schema;
|
|
93
116
|
const { nullable, example, ...cleanRest } = rest;
|
|
94
117
|
const result = { ...cleanRest };
|
|
@@ -140,34 +163,34 @@ function toJsonSchema(schema) {
|
|
|
140
163
|
if (result["properties"] && typeof result["properties"] === "object") {
|
|
141
164
|
const props = {};
|
|
142
165
|
for (const [key, value] of Object.entries(result["properties"])) {
|
|
143
|
-
props[key] =
|
|
166
|
+
props[key] = recurse(value);
|
|
144
167
|
}
|
|
145
168
|
result["properties"] = props;
|
|
146
169
|
}
|
|
147
170
|
if (result["items"]) {
|
|
148
171
|
if (Array.isArray(result["items"])) {
|
|
149
|
-
result["items"] = result["items"].map(
|
|
172
|
+
result["items"] = result["items"].map(recurse);
|
|
150
173
|
} else {
|
|
151
|
-
result["items"] =
|
|
174
|
+
result["items"] = recurse(result["items"]);
|
|
152
175
|
}
|
|
153
176
|
}
|
|
154
177
|
if (result["additionalProperties"] && typeof result["additionalProperties"] === "object") {
|
|
155
|
-
result["additionalProperties"] =
|
|
178
|
+
result["additionalProperties"] = recurse(result["additionalProperties"]);
|
|
156
179
|
}
|
|
157
180
|
for (const key of ["allOf", "anyOf", "oneOf"]) {
|
|
158
181
|
if (result[key] && Array.isArray(result[key])) {
|
|
159
|
-
result[key] = result[key].map(
|
|
182
|
+
result[key] = result[key].map(recurse);
|
|
160
183
|
}
|
|
161
184
|
}
|
|
162
185
|
if (result["not"]) {
|
|
163
|
-
result["not"] =
|
|
186
|
+
result["not"] = recurse(result["not"]);
|
|
164
187
|
}
|
|
165
188
|
for (const key of ["patternProperties", "$defs", "definitions", "dependentSchemas"]) {
|
|
166
189
|
const value = result[key];
|
|
167
190
|
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
168
191
|
const mapped = {};
|
|
169
192
|
for (const [name, sub] of Object.entries(value)) {
|
|
170
|
-
mapped[name] =
|
|
193
|
+
mapped[name] = recurse(sub);
|
|
171
194
|
}
|
|
172
195
|
result[key] = mapped;
|
|
173
196
|
}
|
|
@@ -184,11 +207,11 @@ function toJsonSchema(schema) {
|
|
|
184
207
|
]) {
|
|
185
208
|
const value = result[key];
|
|
186
209
|
if (value && typeof value === "object") {
|
|
187
|
-
result[key] =
|
|
210
|
+
result[key] = recurse(value);
|
|
188
211
|
}
|
|
189
212
|
}
|
|
190
213
|
if (Array.isArray(result["prefixItems"])) {
|
|
191
|
-
result["prefixItems"] = result["prefixItems"].map(
|
|
214
|
+
result["prefixItems"] = result["prefixItems"].map(recurse);
|
|
192
215
|
}
|
|
193
216
|
if (wrapNullable) {
|
|
194
217
|
const wrapper = {};
|
|
@@ -209,8 +232,11 @@ var ParameterResolver = class {
|
|
|
209
232
|
namingStrategy;
|
|
210
233
|
includeExamples;
|
|
211
234
|
constructor(namingStrategy, options) {
|
|
212
|
-
this.namingStrategy =
|
|
213
|
-
|
|
235
|
+
this.namingStrategy = {
|
|
236
|
+
...namingStrategy,
|
|
237
|
+
// Bind a supplied resolver to its own strategy object so class-based
|
|
238
|
+
// strategies keep their `this` (we invoke it off a spread clone).
|
|
239
|
+
conflictResolver: namingStrategy?.conflictResolver ? namingStrategy.conflictResolver.bind(namingStrategy) : this.defaultConflictResolver
|
|
214
240
|
};
|
|
215
241
|
this.includeExamples = options?.includeExamples ?? false;
|
|
216
242
|
}
|
|
@@ -392,6 +418,9 @@ var ParameterResolver = class {
|
|
|
392
418
|
schema["deprecated"] = true;
|
|
393
419
|
}
|
|
394
420
|
schema["x-parameter-location"] = param.location;
|
|
421
|
+
if (param.location === "header") {
|
|
422
|
+
schema["x-mcp-header"] = param.name;
|
|
423
|
+
}
|
|
395
424
|
if (param.style) {
|
|
396
425
|
schema["x-parameter-style"] = param.style;
|
|
397
426
|
}
|
|
@@ -486,6 +515,9 @@ var ParameterResolver = class {
|
|
|
486
515
|
});
|
|
487
516
|
const schemeInInput = includeInInput === true || Array.isArray(includeInInput) && includeInInput.includes(scheme);
|
|
488
517
|
if (schemeInInput) {
|
|
518
|
+
if (paramLocation === "header") {
|
|
519
|
+
schema["x-mcp-header"] = headerKey;
|
|
520
|
+
}
|
|
489
521
|
properties[inputKey] = schema;
|
|
490
522
|
required.push(inputKey);
|
|
491
523
|
}
|
|
@@ -1158,9 +1190,63 @@ function mergeOverrides(base, layer) {
|
|
|
1158
1190
|
...layer.description !== void 0 && { description: layer.description },
|
|
1159
1191
|
...(base.annotations || layer.annotations) && {
|
|
1160
1192
|
annotations: { ...base.annotations, ...layer.annotations }
|
|
1161
|
-
}
|
|
1193
|
+
},
|
|
1194
|
+
...(base.meta || layer.meta) && { meta: { ...base.meta, ...layer.meta } },
|
|
1195
|
+
...layer.icons !== void 0 && { icons: layer.icons }
|
|
1162
1196
|
};
|
|
1163
1197
|
}
|
|
1198
|
+
function cleanseMeta(node, seen) {
|
|
1199
|
+
if (!node || typeof node !== "object") {
|
|
1200
|
+
return node;
|
|
1201
|
+
}
|
|
1202
|
+
if (seen.has(node)) {
|
|
1203
|
+
return void 0;
|
|
1204
|
+
}
|
|
1205
|
+
seen.add(node);
|
|
1206
|
+
try {
|
|
1207
|
+
if (Array.isArray(node)) {
|
|
1208
|
+
return node.map((item) => cleanseMeta(item, seen));
|
|
1209
|
+
}
|
|
1210
|
+
const out = {};
|
|
1211
|
+
for (const [key, value] of Object.entries(node)) {
|
|
1212
|
+
if (key === "__proto__" || key === "constructor" || key === "prototype") continue;
|
|
1213
|
+
out[key] = cleanseMeta(value, seen);
|
|
1214
|
+
}
|
|
1215
|
+
return out;
|
|
1216
|
+
} finally {
|
|
1217
|
+
seen.delete(node);
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
function sanitizeMeta(value) {
|
|
1221
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
1222
|
+
return cleanseMeta(value, /* @__PURE__ */ new Set());
|
|
1223
|
+
}
|
|
1224
|
+
return void 0;
|
|
1225
|
+
}
|
|
1226
|
+
function isAllowedIconSrc(src) {
|
|
1227
|
+
const lower = src.toLowerCase();
|
|
1228
|
+
return lower.startsWith("https:") || lower.startsWith("data:");
|
|
1229
|
+
}
|
|
1230
|
+
function sanitizeIcons(value) {
|
|
1231
|
+
if (!Array.isArray(value)) {
|
|
1232
|
+
return void 0;
|
|
1233
|
+
}
|
|
1234
|
+
const icons = [];
|
|
1235
|
+
for (const entry of value) {
|
|
1236
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry)) continue;
|
|
1237
|
+
const raw = entry;
|
|
1238
|
+
if (typeof raw["src"] !== "string" || !isAllowedIconSrc(raw["src"])) continue;
|
|
1239
|
+
const icon = { src: raw["src"] };
|
|
1240
|
+
if (typeof raw["mimeType"] === "string") {
|
|
1241
|
+
icon.mimeType = raw["mimeType"];
|
|
1242
|
+
}
|
|
1243
|
+
if (Array.isArray(raw["sizes"]) && raw["sizes"].every((s) => typeof s === "string")) {
|
|
1244
|
+
icon.sizes = [...raw["sizes"]];
|
|
1245
|
+
}
|
|
1246
|
+
icons.push(icon);
|
|
1247
|
+
}
|
|
1248
|
+
return icons.length > 0 ? icons : void 0;
|
|
1249
|
+
}
|
|
1164
1250
|
function readXMcp(node) {
|
|
1165
1251
|
return node["x-mcp"];
|
|
1166
1252
|
}
|
|
@@ -1209,16 +1295,24 @@ function extractExtensionOverrides(operation) {
|
|
|
1209
1295
|
name: typeof ext["name"] === "string" ? ext["name"] : void 0,
|
|
1210
1296
|
title: typeof ext["title"] === "string" ? ext["title"] : void 0,
|
|
1211
1297
|
description: typeof ext["description"] === "string" ? ext["description"] : void 0,
|
|
1212
|
-
annotations: pickAnnotations(ext["annotations"])
|
|
1298
|
+
annotations: pickAnnotations(ext["annotations"]),
|
|
1299
|
+
meta: sanitizeMeta(ext["meta"]),
|
|
1300
|
+
icons: sanitizeIcons(ext["icons"])
|
|
1213
1301
|
});
|
|
1214
1302
|
}
|
|
1215
1303
|
const frontmcp = op["x-frontmcp"];
|
|
1216
|
-
if (frontmcp && typeof frontmcp === "object"
|
|
1217
|
-
const
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1304
|
+
if (frontmcp && typeof frontmcp === "object") {
|
|
1305
|
+
const layer = {
|
|
1306
|
+
meta: sanitizeMeta(frontmcp.meta),
|
|
1307
|
+
icons: sanitizeIcons(frontmcp.icons)
|
|
1308
|
+
};
|
|
1309
|
+
if (frontmcp.annotations) {
|
|
1310
|
+
layer.annotations = pickAnnotations(frontmcp.annotations);
|
|
1311
|
+
if (typeof frontmcp.annotations.title === "string") {
|
|
1312
|
+
layer.title = frontmcp.annotations.title;
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
result = mergeOverrides(result, layer);
|
|
1222
1316
|
}
|
|
1223
1317
|
return result;
|
|
1224
1318
|
}
|
|
@@ -1589,6 +1683,13 @@ var RequestBuildError = class extends OpenAPIToolError {
|
|
|
1589
1683
|
super(message, context);
|
|
1590
1684
|
}
|
|
1591
1685
|
};
|
|
1686
|
+
var ArazzoError = class extends OpenAPIToolError {
|
|
1687
|
+
path;
|
|
1688
|
+
constructor(message, context) {
|
|
1689
|
+
super(message, context);
|
|
1690
|
+
this.path = context?.["path"];
|
|
1691
|
+
}
|
|
1692
|
+
};
|
|
1592
1693
|
var SchemaError = class extends OpenAPIToolError {
|
|
1593
1694
|
constructor(message, context) {
|
|
1594
1695
|
super(message, context);
|
|
@@ -2141,7 +2242,8 @@ var Validator = class {
|
|
|
2141
2242
|
code: "NO_PATHS"
|
|
2142
2243
|
});
|
|
2143
2244
|
} else {
|
|
2144
|
-
|
|
2245
|
+
const componentParameters = document.components?.parameters ?? {};
|
|
2246
|
+
this.validatePaths(document.paths, componentParameters, errors, warnings);
|
|
2145
2247
|
}
|
|
2146
2248
|
if (!document.servers || document.servers.length === 0) {
|
|
2147
2249
|
warnings.push({
|
|
@@ -2172,7 +2274,18 @@ var Validator = class {
|
|
|
2172
2274
|
/**
|
|
2173
2275
|
* Validate paths
|
|
2174
2276
|
*/
|
|
2175
|
-
|
|
2277
|
+
/**
|
|
2278
|
+
* Resolve a local `#/components/parameters/<name>` reference (JSON Pointer
|
|
2279
|
+
* tokens decoded). Returns undefined for external or dangling references.
|
|
2280
|
+
*/
|
|
2281
|
+
resolveParameterRef(param, componentParameters) {
|
|
2282
|
+
if (!param || typeof param !== "object" || !("$ref" in param)) return param;
|
|
2283
|
+
const match = /^#\/components\/parameters\/(.+)$/.exec(String(param.$ref));
|
|
2284
|
+
if (!match) return void 0;
|
|
2285
|
+
const name = match[1].replace(/~1/g, "/").replace(/~0/g, "~");
|
|
2286
|
+
return componentParameters[name];
|
|
2287
|
+
}
|
|
2288
|
+
validatePaths(paths, componentParameters, errors, warnings) {
|
|
2176
2289
|
for (const [path, pathItem] of Object.entries(paths)) {
|
|
2177
2290
|
if (!pathItem) continue;
|
|
2178
2291
|
if (!path.startsWith("/")) {
|
|
@@ -2184,11 +2297,15 @@ var Validator = class {
|
|
|
2184
2297
|
}
|
|
2185
2298
|
const methods = ["get", "post", "put", "patch", "delete", "head", "options", "trace"];
|
|
2186
2299
|
let hasOperations = false;
|
|
2300
|
+
const pathLevelParameters = Array.isArray(pathItem.parameters) ? pathItem.parameters : [];
|
|
2301
|
+
if (pathLevelParameters.length > 0) {
|
|
2302
|
+
this.validateParameters(pathLevelParameters, `/paths/${path}/parameters`, errors, warnings);
|
|
2303
|
+
}
|
|
2187
2304
|
for (const method of methods) {
|
|
2188
2305
|
const operation = pathItem[method];
|
|
2189
2306
|
if (operation) {
|
|
2190
2307
|
hasOperations = true;
|
|
2191
|
-
this.validateOperation(operation, path, method, errors, warnings);
|
|
2308
|
+
this.validateOperation(operation, path, method, errors, warnings, pathLevelParameters, componentParameters);
|
|
2192
2309
|
}
|
|
2193
2310
|
}
|
|
2194
2311
|
if (!hasOperations && !pathItem.$ref) {
|
|
@@ -2203,7 +2320,7 @@ var Validator = class {
|
|
|
2203
2320
|
/**
|
|
2204
2321
|
* Validate an operation
|
|
2205
2322
|
*/
|
|
2206
|
-
validateOperation(operation, path, method, errors, warnings) {
|
|
2323
|
+
validateOperation(operation, path, method, errors, warnings, pathLevelParameters = [], componentParameters = {}) {
|
|
2207
2324
|
const basePath = `/paths/${path}/${method}`;
|
|
2208
2325
|
if (!operation.operationId) {
|
|
2209
2326
|
warnings.push({
|
|
@@ -2220,14 +2337,18 @@ var Validator = class {
|
|
|
2220
2337
|
});
|
|
2221
2338
|
}
|
|
2222
2339
|
if (operation.parameters) {
|
|
2223
|
-
this.validateParameters(operation.parameters,
|
|
2340
|
+
this.validateParameters(operation.parameters, `${basePath}/parameters`, errors, warnings);
|
|
2224
2341
|
}
|
|
2342
|
+
const allParameters = [...pathLevelParameters, ...operation.parameters ?? []].map(
|
|
2343
|
+
(p) => this.resolveParameterRef(p, componentParameters)
|
|
2344
|
+
);
|
|
2345
|
+
const hasUnresolvableRefs = allParameters.some((p) => p === void 0);
|
|
2225
2346
|
const pathParams = path.match(/\{([^{}]+)\}/g)?.map((p) => p.slice(1, -1)) ?? [];
|
|
2226
2347
|
const definedPathParams = new Set(
|
|
2227
|
-
|
|
2348
|
+
allParameters.filter((p) => p && p.in === "path").map((p) => p.name)
|
|
2228
2349
|
);
|
|
2229
2350
|
for (const param of pathParams) {
|
|
2230
|
-
if (!definedPathParams.has(param)) {
|
|
2351
|
+
if (!hasUnresolvableRefs && !definedPathParams.has(param)) {
|
|
2231
2352
|
errors.push({
|
|
2232
2353
|
message: `Path parameter '${param}' not defined in parameters: ${method.toUpperCase()} ${path}`,
|
|
2233
2354
|
path: `${basePath}/parameters`,
|
|
@@ -2239,11 +2360,13 @@ var Validator = class {
|
|
|
2239
2360
|
/**
|
|
2240
2361
|
* Validate parameters
|
|
2241
2362
|
*/
|
|
2242
|
-
validateParameters(parameters,
|
|
2243
|
-
const basePath = `/paths/${path}/${method}/parameters`;
|
|
2363
|
+
validateParameters(parameters, basePath, errors, warnings) {
|
|
2244
2364
|
for (let i = 0; i < parameters.length; i++) {
|
|
2245
2365
|
const param = parameters[i];
|
|
2246
2366
|
const paramPath = `${basePath}/${i}`;
|
|
2367
|
+
if (param && typeof param === "object" && "$ref" in param) {
|
|
2368
|
+
continue;
|
|
2369
|
+
}
|
|
2247
2370
|
if (!param.name) {
|
|
2248
2371
|
errors.push({
|
|
2249
2372
|
message: "Parameter missing name",
|
|
@@ -2391,6 +2514,376 @@ function resolveSchemaFormats(schema, resolvers) {
|
|
|
2391
2514
|
return result;
|
|
2392
2515
|
}
|
|
2393
2516
|
|
|
2517
|
+
// src/type-signature.ts
|
|
2518
|
+
var DEFAULT_MAX_DEPTH = 8;
|
|
2519
|
+
var IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
2520
|
+
function toPascalIdentifier(toolName) {
|
|
2521
|
+
const segments = toolName.split(/[^A-Za-z0-9]+/).filter((s) => s.length > 0);
|
|
2522
|
+
const joined = segments.map((s) => s[0].toUpperCase() + s.slice(1)).join("");
|
|
2523
|
+
if (joined === "") {
|
|
2524
|
+
return "Tool";
|
|
2525
|
+
}
|
|
2526
|
+
return /^[0-9]/.test(joined) ? `T${joined}` : joined;
|
|
2527
|
+
}
|
|
2528
|
+
function lowerFirst(name) {
|
|
2529
|
+
return name[0].toLowerCase() + name.slice(1);
|
|
2530
|
+
}
|
|
2531
|
+
function isSchemaRecord(value) {
|
|
2532
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2533
|
+
}
|
|
2534
|
+
function isNullSchema(value) {
|
|
2535
|
+
return isSchemaRecord(value) && value["type"] === "null";
|
|
2536
|
+
}
|
|
2537
|
+
function paren(expr) {
|
|
2538
|
+
return expr.includes(" | ") || expr.includes(" & ") ? `(${expr})` : expr;
|
|
2539
|
+
}
|
|
2540
|
+
function dedupe(parts) {
|
|
2541
|
+
return [...new Set(parts)];
|
|
2542
|
+
}
|
|
2543
|
+
function quoteKey(name) {
|
|
2544
|
+
return IDENTIFIER.test(name) ? name : JSON.stringify(name);
|
|
2545
|
+
}
|
|
2546
|
+
function literalOf(value) {
|
|
2547
|
+
if (value === null) {
|
|
2548
|
+
return "null";
|
|
2549
|
+
}
|
|
2550
|
+
const t = typeof value;
|
|
2551
|
+
if (t === "number") {
|
|
2552
|
+
return Number.isFinite(value) ? JSON.stringify(value) : "number";
|
|
2553
|
+
}
|
|
2554
|
+
if (t === "string" || t === "boolean") {
|
|
2555
|
+
return JSON.stringify(value);
|
|
2556
|
+
}
|
|
2557
|
+
return "unknown";
|
|
2558
|
+
}
|
|
2559
|
+
var RESERVED_WORDS = /* @__PURE__ */ new Set([
|
|
2560
|
+
"break",
|
|
2561
|
+
"case",
|
|
2562
|
+
"catch",
|
|
2563
|
+
"class",
|
|
2564
|
+
"const",
|
|
2565
|
+
"continue",
|
|
2566
|
+
"debugger",
|
|
2567
|
+
"default",
|
|
2568
|
+
"delete",
|
|
2569
|
+
"do",
|
|
2570
|
+
"else",
|
|
2571
|
+
"enum",
|
|
2572
|
+
"export",
|
|
2573
|
+
"extends",
|
|
2574
|
+
"false",
|
|
2575
|
+
"finally",
|
|
2576
|
+
"for",
|
|
2577
|
+
"function",
|
|
2578
|
+
"if",
|
|
2579
|
+
"import",
|
|
2580
|
+
"in",
|
|
2581
|
+
"instanceof",
|
|
2582
|
+
"new",
|
|
2583
|
+
"null",
|
|
2584
|
+
"return",
|
|
2585
|
+
"super",
|
|
2586
|
+
"switch",
|
|
2587
|
+
"this",
|
|
2588
|
+
"throw",
|
|
2589
|
+
"true",
|
|
2590
|
+
"try",
|
|
2591
|
+
"typeof",
|
|
2592
|
+
"var",
|
|
2593
|
+
"void",
|
|
2594
|
+
"while",
|
|
2595
|
+
"with",
|
|
2596
|
+
"implements",
|
|
2597
|
+
"interface",
|
|
2598
|
+
"let",
|
|
2599
|
+
"package",
|
|
2600
|
+
"private",
|
|
2601
|
+
"protected",
|
|
2602
|
+
"public",
|
|
2603
|
+
"static",
|
|
2604
|
+
"yield",
|
|
2605
|
+
"await"
|
|
2606
|
+
]);
|
|
2607
|
+
function escapeJsdoc(text) {
|
|
2608
|
+
return text.replace(/\*\//g, "*\\/");
|
|
2609
|
+
}
|
|
2610
|
+
function jsdocLines(prop) {
|
|
2611
|
+
if (!isSchemaRecord(prop)) {
|
|
2612
|
+
return [];
|
|
2613
|
+
}
|
|
2614
|
+
const lines = [];
|
|
2615
|
+
const description = prop["description"];
|
|
2616
|
+
if (typeof description === "string" && description !== "") {
|
|
2617
|
+
lines.push(...escapeJsdoc(description).split("\n"));
|
|
2618
|
+
}
|
|
2619
|
+
const format = prop["format"];
|
|
2620
|
+
if (typeof format === "string" && format !== "") {
|
|
2621
|
+
lines.push(`@format ${escapeJsdoc(format)}`);
|
|
2622
|
+
}
|
|
2623
|
+
if ("default" in prop && !(typeof prop["default"] === "number" && !Number.isFinite(prop["default"]))) {
|
|
2624
|
+
const rendered = JSON.stringify(prop["default"]);
|
|
2625
|
+
if (rendered !== void 0) {
|
|
2626
|
+
lines.push(`@default ${escapeJsdoc(rendered)}`);
|
|
2627
|
+
}
|
|
2628
|
+
}
|
|
2629
|
+
if (prop["deprecated"] === true) {
|
|
2630
|
+
lines.push("@deprecated");
|
|
2631
|
+
}
|
|
2632
|
+
return lines;
|
|
2633
|
+
}
|
|
2634
|
+
function renderJsdoc(lines, indent) {
|
|
2635
|
+
if (lines.length === 1) {
|
|
2636
|
+
return `${indent}/** ${lines[0]} */
|
|
2637
|
+
`;
|
|
2638
|
+
}
|
|
2639
|
+
return `${indent}/**
|
|
2640
|
+
${lines.map((l) => `${indent} * ${l}`).join("\n")}
|
|
2641
|
+
${indent} */
|
|
2642
|
+
`;
|
|
2643
|
+
}
|
|
2644
|
+
function hasObjectShape(r) {
|
|
2645
|
+
return r["type"] === "object" || r["type"] === void 0 && (r["properties"] !== void 0 || r["additionalProperties"] !== void 0 || r["patternProperties"] !== void 0);
|
|
2646
|
+
}
|
|
2647
|
+
function typeExpr(schema, ctx, depth, indent) {
|
|
2648
|
+
if (schema === true) {
|
|
2649
|
+
return "unknown";
|
|
2650
|
+
}
|
|
2651
|
+
if (schema === false) {
|
|
2652
|
+
return "never";
|
|
2653
|
+
}
|
|
2654
|
+
if (!isSchemaRecord(schema)) {
|
|
2655
|
+
return "unknown";
|
|
2656
|
+
}
|
|
2657
|
+
if (ctx.stack.has(schema)) {
|
|
2658
|
+
return "unknown";
|
|
2659
|
+
}
|
|
2660
|
+
if (depth >= ctx.maxDepth) {
|
|
2661
|
+
return "unknown";
|
|
2662
|
+
}
|
|
2663
|
+
if (schema["$ref"] !== void 0) {
|
|
2664
|
+
return "unknown";
|
|
2665
|
+
}
|
|
2666
|
+
ctx.stack.add(schema);
|
|
2667
|
+
try {
|
|
2668
|
+
return typeExprInner(schema, ctx, depth, indent);
|
|
2669
|
+
} finally {
|
|
2670
|
+
ctx.stack.delete(schema);
|
|
2671
|
+
}
|
|
2672
|
+
}
|
|
2673
|
+
function typeExprInner(r, ctx, depth, indent) {
|
|
2674
|
+
if ("const" in r) {
|
|
2675
|
+
const rendered = literalOf(r["const"]);
|
|
2676
|
+
if (rendered !== "unknown") {
|
|
2677
|
+
return rendered;
|
|
2678
|
+
}
|
|
2679
|
+
}
|
|
2680
|
+
const enumMembers = r["enum"];
|
|
2681
|
+
if (Array.isArray(enumMembers)) {
|
|
2682
|
+
if (enumMembers.length === 0) {
|
|
2683
|
+
return "unknown";
|
|
2684
|
+
}
|
|
2685
|
+
return dedupe(enumMembers.map(literalOf)).join(" | ");
|
|
2686
|
+
}
|
|
2687
|
+
const anyOf = r["anyOf"];
|
|
2688
|
+
if (Array.isArray(anyOf) && anyOf.length === 2) {
|
|
2689
|
+
const nullIdx = anyOf.findIndex(isNullSchema);
|
|
2690
|
+
if (nullIdx >= 0 && !isNullSchema(anyOf[1 - nullIdx])) {
|
|
2691
|
+
return `${paren(typeExpr(anyOf[1 - nullIdx], ctx, depth + 1, indent))} | null`;
|
|
2692
|
+
}
|
|
2693
|
+
}
|
|
2694
|
+
const allOf = r["allOf"];
|
|
2695
|
+
if (Array.isArray(allOf)) {
|
|
2696
|
+
const parts = allOf.map((m) => paren(typeExpr(m, ctx, depth + 1, indent)));
|
|
2697
|
+
if (r["properties"] !== void 0) {
|
|
2698
|
+
parts.push(paren(objectExpr(r, ctx, depth, indent)));
|
|
2699
|
+
}
|
|
2700
|
+
return parts.length === 0 ? "unknown" : dedupe(parts).join(" & ");
|
|
2701
|
+
}
|
|
2702
|
+
const union = Array.isArray(r["oneOf"]) ? r["oneOf"] : Array.isArray(anyOf) ? anyOf : void 0;
|
|
2703
|
+
if (union) {
|
|
2704
|
+
if (union.length === 0) {
|
|
2705
|
+
return "unknown";
|
|
2706
|
+
}
|
|
2707
|
+
return dedupe(union.map((m) => typeExpr(m, ctx, depth + 1, indent))).join(" | ");
|
|
2708
|
+
}
|
|
2709
|
+
const type = r["type"];
|
|
2710
|
+
if (Array.isArray(type)) {
|
|
2711
|
+
const parts = type.filter((t) => typeof t === "string").map((t) => typeExpr({ ...r, type: t }, ctx, depth, indent));
|
|
2712
|
+
return parts.length === 0 ? "unknown" : dedupe(parts).join(" | ");
|
|
2713
|
+
}
|
|
2714
|
+
switch (type) {
|
|
2715
|
+
case "string":
|
|
2716
|
+
return "string";
|
|
2717
|
+
case "number":
|
|
2718
|
+
case "integer":
|
|
2719
|
+
return "number";
|
|
2720
|
+
case "boolean":
|
|
2721
|
+
return "boolean";
|
|
2722
|
+
case "null":
|
|
2723
|
+
return "null";
|
|
2724
|
+
case "array":
|
|
2725
|
+
return arrayExpr(r, ctx, depth, indent);
|
|
2726
|
+
default:
|
|
2727
|
+
if (hasObjectShape(r)) {
|
|
2728
|
+
return objectExpr(r, ctx, depth, indent);
|
|
2729
|
+
}
|
|
2730
|
+
return "unknown";
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
function arrayExpr(r, ctx, depth, indent) {
|
|
2734
|
+
const items = r["items"];
|
|
2735
|
+
const prefix = Array.isArray(r["prefixItems"]) ? r["prefixItems"] : Array.isArray(items) ? items : void 0;
|
|
2736
|
+
if (prefix) {
|
|
2737
|
+
const parts = prefix.map((m) => typeExpr(m, ctx, depth + 1, indent));
|
|
2738
|
+
let rest = "";
|
|
2739
|
+
if (Array.isArray(r["prefixItems"]) && items !== void 0 && !Array.isArray(items)) {
|
|
2740
|
+
rest = `, ...${paren(typeExpr(items, ctx, depth + 1, indent))}[]`;
|
|
2741
|
+
}
|
|
2742
|
+
return `[${parts.join(", ")}${rest}]`;
|
|
2743
|
+
}
|
|
2744
|
+
if (items === void 0) {
|
|
2745
|
+
return "unknown[]";
|
|
2746
|
+
}
|
|
2747
|
+
return `${paren(typeExpr(items, ctx, depth + 1, indent))}[]`;
|
|
2748
|
+
}
|
|
2749
|
+
function objectExpr(r, ctx, depth, indent) {
|
|
2750
|
+
const properties = isSchemaRecord(r["properties"]) ? r["properties"] : {};
|
|
2751
|
+
const entries = Object.entries(properties);
|
|
2752
|
+
const required = new Set(Array.isArray(r["required"]) ? r["required"] : []);
|
|
2753
|
+
const extraTypes = [];
|
|
2754
|
+
const ap = r["additionalProperties"];
|
|
2755
|
+
if (ap === true) {
|
|
2756
|
+
extraTypes.push("unknown");
|
|
2757
|
+
} else if (isSchemaRecord(ap)) {
|
|
2758
|
+
extraTypes.push(typeExpr(ap, ctx, depth + 1, indent));
|
|
2759
|
+
}
|
|
2760
|
+
const patternProps = r["patternProperties"];
|
|
2761
|
+
if (isSchemaRecord(patternProps)) {
|
|
2762
|
+
for (const value of Object.values(patternProps)) {
|
|
2763
|
+
extraTypes.push(typeExpr(value, ctx, depth + 1, indent));
|
|
2764
|
+
}
|
|
2765
|
+
}
|
|
2766
|
+
const extra = extraTypes.length > 0 ? dedupe(extraTypes).join(" | ") : void 0;
|
|
2767
|
+
if (entries.length === 0) {
|
|
2768
|
+
if (extra !== void 0) {
|
|
2769
|
+
return `Record<string, ${extra}>`;
|
|
2770
|
+
}
|
|
2771
|
+
return ap === false ? "Record<string, never>" : "Record<string, unknown>";
|
|
2772
|
+
}
|
|
2773
|
+
const suffix = extra !== void 0 ? ` & Record<string, ${extra}>` : "";
|
|
2774
|
+
if (ctx.mode === "compact") {
|
|
2775
|
+
const members = entries.map(
|
|
2776
|
+
([key, prop]) => `${quoteKey(key)}${required.has(key) ? "" : "?"}: ${typeExpr(prop, ctx, depth + 1, indent)}`
|
|
2777
|
+
);
|
|
2778
|
+
return `{ ${members.join("; ")} }${suffix}`;
|
|
2779
|
+
}
|
|
2780
|
+
const inner = indent + " ";
|
|
2781
|
+
let body = "{\n";
|
|
2782
|
+
for (const [key, prop] of entries) {
|
|
2783
|
+
const doc = jsdocLines(prop);
|
|
2784
|
+
if (doc.length > 0) {
|
|
2785
|
+
body += renderJsdoc(doc, inner);
|
|
2786
|
+
}
|
|
2787
|
+
body += `${inner}${quoteKey(key)}${required.has(key) ? "" : "?"}: ${typeExpr(prop, ctx, depth + 1, inner)};
|
|
2788
|
+
`;
|
|
2789
|
+
}
|
|
2790
|
+
body += `${indent}}`;
|
|
2791
|
+
return `${body}${suffix}`;
|
|
2792
|
+
}
|
|
2793
|
+
function isPlainObjectBody(schema) {
|
|
2794
|
+
if (!isSchemaRecord(schema) || schema["$ref"] !== void 0) {
|
|
2795
|
+
return false;
|
|
2796
|
+
}
|
|
2797
|
+
if ("const" in schema && literalOf(schema["const"]) !== "unknown" || Array.isArray(schema["enum"])) {
|
|
2798
|
+
return false;
|
|
2799
|
+
}
|
|
2800
|
+
if (Array.isArray(schema["allOf"]) || Array.isArray(schema["oneOf"]) || Array.isArray(schema["anyOf"])) {
|
|
2801
|
+
return false;
|
|
2802
|
+
}
|
|
2803
|
+
if (Array.isArray(schema["type"]) || !hasObjectShape(schema)) {
|
|
2804
|
+
return false;
|
|
2805
|
+
}
|
|
2806
|
+
const properties = isSchemaRecord(schema["properties"]) ? schema["properties"] : {};
|
|
2807
|
+
if (Object.keys(properties).length === 0) {
|
|
2808
|
+
return false;
|
|
2809
|
+
}
|
|
2810
|
+
const ap = schema["additionalProperties"];
|
|
2811
|
+
if (ap === true || isSchemaRecord(ap) || isSchemaRecord(schema["patternProperties"])) {
|
|
2812
|
+
return false;
|
|
2813
|
+
}
|
|
2814
|
+
return true;
|
|
2815
|
+
}
|
|
2816
|
+
function namedRoot(name, schema, ctx) {
|
|
2817
|
+
const expr = typeExpr(schema, ctx, 0, "");
|
|
2818
|
+
return isPlainObjectBody(schema) ? `interface ${name} ${expr}` : `type ${name} = ${expr};`;
|
|
2819
|
+
}
|
|
2820
|
+
function paramList(inputSchema, typeText) {
|
|
2821
|
+
if (inputSchema === true) {
|
|
2822
|
+
return `(input?: ${typeText})`;
|
|
2823
|
+
}
|
|
2824
|
+
if (!isSchemaRecord(inputSchema)) {
|
|
2825
|
+
return "()";
|
|
2826
|
+
}
|
|
2827
|
+
const properties = isSchemaRecord(inputSchema["properties"]) ? inputSchema["properties"] : {};
|
|
2828
|
+
const keys = Object.keys(properties);
|
|
2829
|
+
if (keys.length === 0) {
|
|
2830
|
+
const ap = inputSchema["additionalProperties"];
|
|
2831
|
+
const hasExtra = ap === true || isSchemaRecord(ap) || isSchemaRecord(inputSchema["patternProperties"]);
|
|
2832
|
+
const objectish = inputSchema["type"] === "object" || inputSchema["type"] === void 0;
|
|
2833
|
+
const composed = Array.isArray(inputSchema["allOf"]) || Array.isArray(inputSchema["oneOf"]) || Array.isArray(inputSchema["anyOf"]) || Array.isArray(inputSchema["enum"]) || "const" in inputSchema;
|
|
2834
|
+
return objectish && !hasExtra && !composed ? "()" : `(input: ${typeText})`;
|
|
2835
|
+
}
|
|
2836
|
+
const required = new Set(Array.isArray(inputSchema["required"]) ? inputSchema["required"] : []);
|
|
2837
|
+
const allOptional = keys.every((k) => !required.has(k));
|
|
2838
|
+
return allOptional ? `(input?: ${typeText})` : `(input: ${typeText})`;
|
|
2839
|
+
}
|
|
2840
|
+
function outputVariantsDeclaration(name, variants, ctx) {
|
|
2841
|
+
const lines = variants.map((member) => {
|
|
2842
|
+
let comment = "";
|
|
2843
|
+
if (isSchemaRecord(member)) {
|
|
2844
|
+
const status = member["x-status-code"];
|
|
2845
|
+
if (typeof status === "number" || typeof status === "string") {
|
|
2846
|
+
const contentType = member["x-content-type"];
|
|
2847
|
+
const ct = typeof contentType === "string" ? ` (${escapeJsdoc(contentType)})` : "";
|
|
2848
|
+
comment = `/** status ${escapeJsdoc(String(status))}${ct} */ `;
|
|
2849
|
+
}
|
|
2850
|
+
}
|
|
2851
|
+
return ` | ${comment}${typeExpr(member, ctx, 1, " ")}`;
|
|
2852
|
+
});
|
|
2853
|
+
return `type ${name} =
|
|
2854
|
+
${lines.join("\n")};`;
|
|
2855
|
+
}
|
|
2856
|
+
function emitToolTypeScript(toolName, description, inputSchema, outputSchema, options = {}) {
|
|
2857
|
+
const maxDepth = typeof options.maxDepth === "number" && Number.isFinite(options.maxDepth) ? Math.max(1, Math.floor(options.maxDepth)) : DEFAULT_MAX_DEPTH;
|
|
2858
|
+
const compact = { mode: "compact", maxDepth, stack: /* @__PURE__ */ new Set() };
|
|
2859
|
+
const pretty = { mode: "pretty", maxDepth, stack: /* @__PURE__ */ new Set() };
|
|
2860
|
+
const inputCompact = typeExpr(inputSchema, compact, 0, "");
|
|
2861
|
+
const outputCompact = outputSchema === void 0 ? "unknown" : typeExpr(outputSchema, compact, 0, "");
|
|
2862
|
+
const signature = `${paramList(inputSchema, inputCompact)} => Promise<${outputCompact}>`;
|
|
2863
|
+
const base = toPascalIdentifier(toolName);
|
|
2864
|
+
const inputName = `${base}Input`;
|
|
2865
|
+
const outputName = `${base}Output`;
|
|
2866
|
+
const blocks = [];
|
|
2867
|
+
if (typeof description === "string" && description !== "") {
|
|
2868
|
+
blocks.push(renderJsdoc(escapeJsdoc(description).split("\n"), "").trimEnd());
|
|
2869
|
+
}
|
|
2870
|
+
blocks.push(namedRoot(inputName, inputSchema, pretty));
|
|
2871
|
+
const outputUnion = isSchemaRecord(outputSchema) && Array.isArray(outputSchema["oneOf"]) ? outputSchema["oneOf"] : void 0;
|
|
2872
|
+
if (outputSchema === void 0) {
|
|
2873
|
+
blocks.push(`type ${outputName} = unknown;`);
|
|
2874
|
+
} else if (outputUnion && outputUnion.some((m) => isSchemaRecord(m) && m["x-status-code"] !== void 0)) {
|
|
2875
|
+
blocks.push(outputVariantsDeclaration(outputName, outputUnion, pretty));
|
|
2876
|
+
} else {
|
|
2877
|
+
blocks.push(namedRoot(outputName, outputSchema, pretty));
|
|
2878
|
+
}
|
|
2879
|
+
let fnName = lowerFirst(base);
|
|
2880
|
+
if (RESERVED_WORDS.has(fnName)) {
|
|
2881
|
+
fnName = `${fnName}_`;
|
|
2882
|
+
}
|
|
2883
|
+
blocks.push(`declare function ${fnName}${paramList(inputSchema, inputName)}: Promise<${outputName}>;`);
|
|
2884
|
+
return { signature, declaration: blocks.join("\n\n") };
|
|
2885
|
+
}
|
|
2886
|
+
|
|
2394
2887
|
// src/ssrf.ts
|
|
2395
2888
|
var BLOCKED_HOSTNAMES = /* @__PURE__ */ new Set([
|
|
2396
2889
|
"localhost",
|
|
@@ -2799,6 +3292,25 @@ function globToRegExp(glob) {
|
|
|
2799
3292
|
function matchesAnyGlob(path, globs) {
|
|
2800
3293
|
return globs.some((glob) => globToRegExp(glob).test(path));
|
|
2801
3294
|
}
|
|
3295
|
+
function iconsFromInfoLogo(info) {
|
|
3296
|
+
if (!info || typeof info !== "object") {
|
|
3297
|
+
return void 0;
|
|
3298
|
+
}
|
|
3299
|
+
const logo = info["x-logo"];
|
|
3300
|
+
let src;
|
|
3301
|
+
if (typeof logo === "string") {
|
|
3302
|
+
src = logo;
|
|
3303
|
+
} else if (logo && typeof logo === "object" && !Array.isArray(logo)) {
|
|
3304
|
+
const url = logo["url"];
|
|
3305
|
+
if (typeof url === "string") {
|
|
3306
|
+
src = url;
|
|
3307
|
+
}
|
|
3308
|
+
}
|
|
3309
|
+
if (src !== void 0 && isAllowedIconSrc(src)) {
|
|
3310
|
+
return [{ src }];
|
|
3311
|
+
}
|
|
3312
|
+
return void 0;
|
|
3313
|
+
}
|
|
2802
3314
|
function trimUnderscores(value) {
|
|
2803
3315
|
let start = 0;
|
|
2804
3316
|
let end = value.length;
|
|
@@ -3181,6 +3693,14 @@ var OpenAPIToolGenerator = class _OpenAPIToolGenerator {
|
|
|
3181
3693
|
attempts++;
|
|
3182
3694
|
}
|
|
3183
3695
|
tool = { ...tool, name: deduped };
|
|
3696
|
+
if (tool.metadata.typescript) {
|
|
3697
|
+
tool.metadata = {
|
|
3698
|
+
...tool.metadata,
|
|
3699
|
+
typescript: emitToolTypeScript(deduped, tool.description, tool.inputSchema, tool.outputSchema, {
|
|
3700
|
+
maxDepth: Math.max(1, options.maxSchemaDepth ?? 10)
|
|
3701
|
+
})
|
|
3702
|
+
};
|
|
3703
|
+
}
|
|
3184
3704
|
}
|
|
3185
3705
|
usedNames.add(tool.name);
|
|
3186
3706
|
tools.push(tool);
|
|
@@ -3229,7 +3749,13 @@ var OpenAPIToolGenerator = class _OpenAPIToolGenerator {
|
|
|
3229
3749
|
const responseBuilder = new ResponseBuilder(options);
|
|
3230
3750
|
const outputSchema = responseBuilder.build(operation.responses);
|
|
3231
3751
|
const overrides = extractExtensionOverrides(operation);
|
|
3232
|
-
const name = this.generateToolName(
|
|
3752
|
+
const name = this.generateToolName(
|
|
3753
|
+
pathStr,
|
|
3754
|
+
method,
|
|
3755
|
+
overrides.name ?? operation.operationId,
|
|
3756
|
+
options,
|
|
3757
|
+
operation
|
|
3758
|
+
);
|
|
3233
3759
|
const description = overrides.description ?? composeDescription(operation, method, pathStr, options.descriptionStrategy ?? "summaryOnly");
|
|
3234
3760
|
const title = overrides.title ?? operation.summary;
|
|
3235
3761
|
const inferred = options.inferAnnotations !== false ? inferAnnotationsFromMethod(method.toLowerCase()) : void 0;
|
|
@@ -3294,11 +3820,48 @@ var OpenAPIToolGenerator = class _OpenAPIToolGenerator {
|
|
|
3294
3820
|
Returns: ${summary}`;
|
|
3295
3821
|
}
|
|
3296
3822
|
}
|
|
3823
|
+
if (options.emitTypeSignatures) {
|
|
3824
|
+
metadata.typescript = emitToolTypeScript(name, finalDescription, resolvedInputSchema, resolvedOutputSchema, {
|
|
3825
|
+
// Print at least as deep as the schemas were truncated, so the
|
|
3826
|
+
// emitted types never collapse levels the schema still carries.
|
|
3827
|
+
maxDepth: Math.max(1, options.maxSchemaDepth ?? 10)
|
|
3828
|
+
});
|
|
3829
|
+
}
|
|
3830
|
+
let toolMeta;
|
|
3831
|
+
if (overrides.meta) {
|
|
3832
|
+
toolMeta = {};
|
|
3833
|
+
for (const [key, value] of Object.entries(overrides.meta)) {
|
|
3834
|
+
if (!key.startsWith("dev.agentfront.openapi/")) {
|
|
3835
|
+
toolMeta[key] = value;
|
|
3836
|
+
}
|
|
3837
|
+
}
|
|
3838
|
+
}
|
|
3839
|
+
if (options.emitMeta) {
|
|
3840
|
+
const info = document.info;
|
|
3841
|
+
toolMeta = {
|
|
3842
|
+
...toolMeta,
|
|
3843
|
+
"dev.agentfront.openapi/operation": {
|
|
3844
|
+
path: pathStr,
|
|
3845
|
+
method,
|
|
3846
|
+
...operation.operationId !== void 0 && { operationId: operation.operationId },
|
|
3847
|
+
...operation.tags && { tags: [...operation.tags] },
|
|
3848
|
+
...operation.deprecated !== void 0 && { deprecated: operation.deprecated },
|
|
3849
|
+
...typeof info?.["title"] === "string" && { specTitle: info["title"] },
|
|
3850
|
+
...typeof info?.["version"] === "string" && { specVersion: info["version"] }
|
|
3851
|
+
}
|
|
3852
|
+
};
|
|
3853
|
+
}
|
|
3854
|
+
if (toolMeta && Object.keys(toolMeta).length === 0) {
|
|
3855
|
+
toolMeta = void 0;
|
|
3856
|
+
}
|
|
3857
|
+
const icons = overrides.icons ?? (options.inheritDocumentIcons ? iconsFromInfoLogo(document.info) : void 0);
|
|
3297
3858
|
return {
|
|
3298
3859
|
name,
|
|
3299
3860
|
...title !== void 0 && { title },
|
|
3300
3861
|
description: finalDescription,
|
|
3301
3862
|
...annotations && { annotations },
|
|
3863
|
+
...toolMeta && { _meta: toolMeta },
|
|
3864
|
+
...icons && { icons },
|
|
3302
3865
|
inputSchema: resolvedInputSchema,
|
|
3303
3866
|
outputSchema: resolvedOutputSchema,
|
|
3304
3867
|
mapper,
|
|
@@ -3366,10 +3929,10 @@ Returns: ${summary}`;
|
|
|
3366
3929
|
/**
|
|
3367
3930
|
* Generate a tool name
|
|
3368
3931
|
*/
|
|
3369
|
-
generateToolName(path, method, operationId, options = {}) {
|
|
3932
|
+
generateToolName(path, method, operationId, options = {}, operation) {
|
|
3370
3933
|
let rawName;
|
|
3371
3934
|
if (options.namingStrategy?.toolNameGenerator) {
|
|
3372
|
-
rawName = options.namingStrategy.toolNameGenerator(path, method, operationId);
|
|
3935
|
+
rawName = options.namingStrategy.toolNameGenerator(path, method, operationId, operation);
|
|
3373
3936
|
} else if (operationId) {
|
|
3374
3937
|
rawName = operationId;
|
|
3375
3938
|
} else {
|
|
@@ -3707,6 +4270,1213 @@ function createSecurityContext(auth) {
|
|
|
3707
4270
|
};
|
|
3708
4271
|
}
|
|
3709
4272
|
|
|
4273
|
+
// src/naming-presets.ts
|
|
4274
|
+
var CODECALL_RESERVED_NAMESPACES = [
|
|
4275
|
+
"console",
|
|
4276
|
+
"Math",
|
|
4277
|
+
"JSON",
|
|
4278
|
+
"Object",
|
|
4279
|
+
"Promise",
|
|
4280
|
+
"Array",
|
|
4281
|
+
"String",
|
|
4282
|
+
"Number",
|
|
4283
|
+
"Boolean",
|
|
4284
|
+
"Date",
|
|
4285
|
+
"RegExp",
|
|
4286
|
+
"Error",
|
|
4287
|
+
"Symbol",
|
|
4288
|
+
"Map",
|
|
4289
|
+
"Set",
|
|
4290
|
+
"WeakMap",
|
|
4291
|
+
"WeakSet",
|
|
4292
|
+
"globalThis",
|
|
4293
|
+
"global",
|
|
4294
|
+
"window",
|
|
4295
|
+
"self",
|
|
4296
|
+
"undefined",
|
|
4297
|
+
"null",
|
|
4298
|
+
"true",
|
|
4299
|
+
"false",
|
|
4300
|
+
"NaN",
|
|
4301
|
+
"Infinity",
|
|
4302
|
+
"callTool",
|
|
4303
|
+
"getTool",
|
|
4304
|
+
"mcpLog",
|
|
4305
|
+
"mcpNotify"
|
|
4306
|
+
];
|
|
4307
|
+
function sanitizeIdentifier(value) {
|
|
4308
|
+
if (value === void 0) {
|
|
4309
|
+
return "";
|
|
4310
|
+
}
|
|
4311
|
+
let out = value.replace(/[^A-Za-z0-9_]+/g, "_").replace(/_+/g, "_");
|
|
4312
|
+
let start = 0;
|
|
4313
|
+
let end = out.length;
|
|
4314
|
+
while (start < end && out[start] === "_") start++;
|
|
4315
|
+
while (end > start && out[end - 1] === "_") end--;
|
|
4316
|
+
out = out.slice(start, end);
|
|
4317
|
+
if (out === "") {
|
|
4318
|
+
return "";
|
|
4319
|
+
}
|
|
4320
|
+
return /^[0-9]/.test(out) ? `_${out}` : out;
|
|
4321
|
+
}
|
|
4322
|
+
function firstPathSegment(path) {
|
|
4323
|
+
for (const segment of path.split("/")) {
|
|
4324
|
+
if (segment !== "" && !segment.startsWith("{")) {
|
|
4325
|
+
return sanitizeIdentifier(segment);
|
|
4326
|
+
}
|
|
4327
|
+
}
|
|
4328
|
+
return "";
|
|
4329
|
+
}
|
|
4330
|
+
function pathMethodHalf(method, path, ns) {
|
|
4331
|
+
const segments = path.split("/").filter((s) => s !== "").map((s) => {
|
|
4332
|
+
const templated = s.replace(/\{([^{}]+)\}/g, "by_$1");
|
|
4333
|
+
return sanitizeIdentifier(templated);
|
|
4334
|
+
}).filter((s) => s !== "");
|
|
4335
|
+
if (segments.length > 0 && segments[0] === ns) {
|
|
4336
|
+
segments.shift();
|
|
4337
|
+
}
|
|
4338
|
+
const joined = segments.join("_");
|
|
4339
|
+
return joined === "" ? method : `${method}_${joined}`;
|
|
4340
|
+
}
|
|
4341
|
+
function dottedNaming(options = {}) {
|
|
4342
|
+
const namespaceFrom = options.namespaceFrom ?? "tag";
|
|
4343
|
+
const reserved = /* @__PURE__ */ new Set([...CODECALL_RESERVED_NAMESPACES, ...options.reservedNamespaces ?? []]);
|
|
4344
|
+
return {
|
|
4345
|
+
toolNameGenerator: (path, method, operationId, operation) => {
|
|
4346
|
+
let ns = "";
|
|
4347
|
+
if (namespaceFrom === "tag") {
|
|
4348
|
+
ns = sanitizeIdentifier(operation?.tags?.[0]);
|
|
4349
|
+
}
|
|
4350
|
+
if (ns === "") {
|
|
4351
|
+
ns = firstPathSegment(path);
|
|
4352
|
+
}
|
|
4353
|
+
if (ns === "") {
|
|
4354
|
+
ns = "api";
|
|
4355
|
+
}
|
|
4356
|
+
if (ns.startsWith("_")) {
|
|
4357
|
+
ns = `n${ns.slice(1)}`;
|
|
4358
|
+
}
|
|
4359
|
+
if (reserved.has(ns)) {
|
|
4360
|
+
ns = `${ns}_`;
|
|
4361
|
+
}
|
|
4362
|
+
const methodHalf = sanitizeIdentifier(operationId) || pathMethodHalf(method, path, ns);
|
|
4363
|
+
return `${ns}.${methodHalf}`;
|
|
4364
|
+
}
|
|
4365
|
+
};
|
|
4366
|
+
}
|
|
4367
|
+
|
|
4368
|
+
// src/elicitation.ts
|
|
4369
|
+
function buildElicitation(source) {
|
|
4370
|
+
const { scheme, type } = source;
|
|
4371
|
+
if (type === "http") {
|
|
4372
|
+
const httpScheme = (source.httpScheme ?? "bearer").toLowerCase();
|
|
4373
|
+
if (httpScheme === "basic" || httpScheme === "digest") {
|
|
4374
|
+
return {
|
|
4375
|
+
scheme,
|
|
4376
|
+
message: `Provide HTTP ${httpScheme} credentials for "${scheme}".`,
|
|
4377
|
+
requestedSchema: {
|
|
4378
|
+
type: "object",
|
|
4379
|
+
properties: {
|
|
4380
|
+
username: { type: "string", title: "Username" },
|
|
4381
|
+
password: { type: "string", title: "Password", description: "Handled as a secret \u2014 never logged." }
|
|
4382
|
+
},
|
|
4383
|
+
required: ["username", "password"]
|
|
4384
|
+
}
|
|
4385
|
+
};
|
|
4386
|
+
}
|
|
4387
|
+
const format = source.bearerFormat ? ` (${source.bearerFormat})` : "";
|
|
4388
|
+
return {
|
|
4389
|
+
scheme,
|
|
4390
|
+
message: `Provide the ${httpScheme} token for "${scheme}".`,
|
|
4391
|
+
requestedSchema: {
|
|
4392
|
+
type: "object",
|
|
4393
|
+
properties: {
|
|
4394
|
+
token: { type: "string", title: "Token", description: `HTTP ${httpScheme} authentication token${format}.` }
|
|
4395
|
+
},
|
|
4396
|
+
required: ["token"]
|
|
4397
|
+
}
|
|
4398
|
+
};
|
|
4399
|
+
}
|
|
4400
|
+
if (type === "apiKey") {
|
|
4401
|
+
const keyName = source.apiKeyName ?? scheme;
|
|
4402
|
+
const location = source.apiKeyIn ?? "header";
|
|
4403
|
+
return {
|
|
4404
|
+
scheme,
|
|
4405
|
+
message: `Provide the API key for "${scheme}".`,
|
|
4406
|
+
requestedSchema: {
|
|
4407
|
+
type: "object",
|
|
4408
|
+
properties: {
|
|
4409
|
+
apiKey: { type: "string", title: "API key", description: `API key "${keyName}" sent via ${location}.` }
|
|
4410
|
+
},
|
|
4411
|
+
required: ["apiKey"]
|
|
4412
|
+
}
|
|
4413
|
+
};
|
|
4414
|
+
}
|
|
4415
|
+
if (type === "oauth2" || type === "openIdConnect") {
|
|
4416
|
+
const scopes = source.scopes && source.scopes.length > 0 ? ` Scopes: ${source.scopes.join(", ")}.` : "";
|
|
4417
|
+
return {
|
|
4418
|
+
scheme,
|
|
4419
|
+
message: `Provide an OAuth2 access token for "${scheme}".${scopes}`,
|
|
4420
|
+
requestedSchema: {
|
|
4421
|
+
type: "object",
|
|
4422
|
+
properties: {
|
|
4423
|
+
accessToken: { type: "string", title: "Access token", description: `OAuth2 access token.${scopes}` }
|
|
4424
|
+
},
|
|
4425
|
+
required: ["accessToken"]
|
|
4426
|
+
}
|
|
4427
|
+
};
|
|
4428
|
+
}
|
|
4429
|
+
return void 0;
|
|
4430
|
+
}
|
|
4431
|
+
function deriveSecurityElicitations(tool) {
|
|
4432
|
+
const sources = [];
|
|
4433
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4434
|
+
for (const entry of tool.mapper) {
|
|
4435
|
+
const security = entry.security;
|
|
4436
|
+
if (security && !seen.has(security.scheme)) {
|
|
4437
|
+
seen.add(security.scheme);
|
|
4438
|
+
sources.push(security);
|
|
4439
|
+
}
|
|
4440
|
+
}
|
|
4441
|
+
if (sources.length === 0 && tool.metadata.security) {
|
|
4442
|
+
for (const requirement of tool.metadata.security) {
|
|
4443
|
+
if (!seen.has(requirement.scheme)) {
|
|
4444
|
+
seen.add(requirement.scheme);
|
|
4445
|
+
sources.push({
|
|
4446
|
+
scheme: requirement.scheme,
|
|
4447
|
+
type: requirement.type,
|
|
4448
|
+
httpScheme: requirement.httpScheme,
|
|
4449
|
+
bearerFormat: requirement.bearerFormat,
|
|
4450
|
+
scopes: requirement.scopes,
|
|
4451
|
+
apiKeyName: requirement.name,
|
|
4452
|
+
apiKeyIn: requirement.in
|
|
4453
|
+
});
|
|
4454
|
+
}
|
|
4455
|
+
}
|
|
4456
|
+
}
|
|
4457
|
+
const result = [];
|
|
4458
|
+
for (const source of sources) {
|
|
4459
|
+
const elicitation = buildElicitation(source);
|
|
4460
|
+
if (elicitation) {
|
|
4461
|
+
result.push(elicitation);
|
|
4462
|
+
}
|
|
4463
|
+
}
|
|
4464
|
+
return result;
|
|
4465
|
+
}
|
|
4466
|
+
|
|
4467
|
+
// src/arazzo-expressions.ts
|
|
4468
|
+
var EXACT_ROOTS = {
|
|
4469
|
+
$url: "url",
|
|
4470
|
+
$method: "method",
|
|
4471
|
+
$statusCode: "statusCode"
|
|
4472
|
+
};
|
|
4473
|
+
var DOTTED_ROOTS = {
|
|
4474
|
+
$inputs: "inputs",
|
|
4475
|
+
$outputs: "outputs",
|
|
4476
|
+
$steps: "steps",
|
|
4477
|
+
$workflows: "workflows",
|
|
4478
|
+
$sourceDescriptions: "sourceDescriptions",
|
|
4479
|
+
$components: "components"
|
|
4480
|
+
};
|
|
4481
|
+
var KNOWN_ROOT = /^\$(?:(?:url|method|statusCode)$|(?:request|response|message)\.|(?:inputs|outputs|steps|workflows|sourceDescriptions|components)\.)/;
|
|
4482
|
+
function fail(message, docPath, expression) {
|
|
4483
|
+
throw new ArazzoError(message, { path: docPath, expression });
|
|
4484
|
+
}
|
|
4485
|
+
var TOKEN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
4486
|
+
function parseSourceRef(prefix, rest, raw, docPath) {
|
|
4487
|
+
if (rest.startsWith("header.")) {
|
|
4488
|
+
const name = rest.slice("header.".length);
|
|
4489
|
+
if (name === "" || !TOKEN.test(name)) {
|
|
4490
|
+
fail(`Invalid header name in runtime expression "${raw}"`, docPath, raw);
|
|
4491
|
+
}
|
|
4492
|
+
return { type: prefix, raw, path: [], source: "header", name };
|
|
4493
|
+
}
|
|
4494
|
+
if (rest.startsWith("query.") || rest.startsWith("path.")) {
|
|
4495
|
+
const source = rest.startsWith("query.") ? "query" : "path";
|
|
4496
|
+
const name = rest.slice(source.length + 1);
|
|
4497
|
+
if (name === "") {
|
|
4498
|
+
fail(`Empty ${source} parameter name in runtime expression "${raw}"`, docPath, raw);
|
|
4499
|
+
}
|
|
4500
|
+
return { type: prefix, raw, path: [], source, name };
|
|
4501
|
+
}
|
|
4502
|
+
if (rest === "body" || rest.startsWith("body#")) {
|
|
4503
|
+
const node = { type: prefix, raw, path: [], source: "body" };
|
|
4504
|
+
if (rest.startsWith("body#")) {
|
|
4505
|
+
const pointer = rest.slice("body#".length);
|
|
4506
|
+
if (pointer !== "" && !pointer.startsWith("/")) {
|
|
4507
|
+
fail(`JSON Pointer in "${raw}" must be empty or start with "/"`, docPath, raw);
|
|
4508
|
+
}
|
|
4509
|
+
node.pointer = pointer;
|
|
4510
|
+
}
|
|
4511
|
+
return node;
|
|
4512
|
+
}
|
|
4513
|
+
fail(`Invalid $${prefix} reference "${raw}" \u2014 expected header.<name>, query.<name>, path.<name>, or body[#<pointer>]`, docPath, raw);
|
|
4514
|
+
}
|
|
4515
|
+
function parseRuntimeExpression(raw, docPath = "") {
|
|
4516
|
+
const exact = EXACT_ROOTS[raw];
|
|
4517
|
+
if (exact) {
|
|
4518
|
+
return { type: exact, raw, path: [] };
|
|
4519
|
+
}
|
|
4520
|
+
for (const key of Object.keys(EXACT_ROOTS)) {
|
|
4521
|
+
if (raw.startsWith(key) && raw !== key) {
|
|
4522
|
+
fail(`Unexpected characters after "${key}" in runtime expression "${raw}"`, docPath, raw);
|
|
4523
|
+
}
|
|
4524
|
+
}
|
|
4525
|
+
for (const prefix of ["request", "response", "message"]) {
|
|
4526
|
+
if (raw.startsWith(`$${prefix}.`)) {
|
|
4527
|
+
return parseSourceRef(prefix, raw.slice(prefix.length + 2), raw, docPath);
|
|
4528
|
+
}
|
|
4529
|
+
}
|
|
4530
|
+
const dot = raw.indexOf(".");
|
|
4531
|
+
const rootToken = dot === -1 ? raw : raw.slice(0, dot);
|
|
4532
|
+
const root = DOTTED_ROOTS[rootToken];
|
|
4533
|
+
if (root) {
|
|
4534
|
+
const rest = dot === -1 ? "" : raw.slice(dot + 1);
|
|
4535
|
+
if (rest === "") {
|
|
4536
|
+
fail(`Runtime expression "${raw}" is missing a name after "${rootToken}."`, docPath, raw);
|
|
4537
|
+
}
|
|
4538
|
+
const path = rest.split(".");
|
|
4539
|
+
if (path.some((segment) => segment === "" || /\s/.test(segment))) {
|
|
4540
|
+
fail(`Runtime expression "${raw}" contains an empty or whitespace path segment`, docPath, raw);
|
|
4541
|
+
}
|
|
4542
|
+
return { type: root, raw, path };
|
|
4543
|
+
}
|
|
4544
|
+
fail(`Invalid runtime expression "${raw}"`, docPath, raw);
|
|
4545
|
+
}
|
|
4546
|
+
function parseExpressionValue(value, docPath = "") {
|
|
4547
|
+
if (typeof value !== "string") {
|
|
4548
|
+
return { kind: "literal", value };
|
|
4549
|
+
}
|
|
4550
|
+
if (value.startsWith("$")) {
|
|
4551
|
+
if (KNOWN_ROOT.test(value)) {
|
|
4552
|
+
return { kind: "expression", expression: parseRuntimeExpression(value, docPath) };
|
|
4553
|
+
}
|
|
4554
|
+
return { kind: "literal", value };
|
|
4555
|
+
}
|
|
4556
|
+
if (!value.includes("{$")) {
|
|
4557
|
+
return { kind: "literal", value };
|
|
4558
|
+
}
|
|
4559
|
+
const parts = [];
|
|
4560
|
+
let cursor = 0;
|
|
4561
|
+
while (cursor < value.length) {
|
|
4562
|
+
const open = value.indexOf("{$", cursor);
|
|
4563
|
+
if (open === -1) {
|
|
4564
|
+
parts.push(value.slice(cursor));
|
|
4565
|
+
break;
|
|
4566
|
+
}
|
|
4567
|
+
if (open > cursor) {
|
|
4568
|
+
parts.push(value.slice(cursor, open));
|
|
4569
|
+
}
|
|
4570
|
+
const close = value.indexOf("}", open);
|
|
4571
|
+
if (close === -1) {
|
|
4572
|
+
fail(`Unterminated "{$" template expression in "${value}"`, docPath, value);
|
|
4573
|
+
}
|
|
4574
|
+
parts.push(parseRuntimeExpression(value.slice(open + 1, close), docPath));
|
|
4575
|
+
cursor = close + 1;
|
|
4576
|
+
}
|
|
4577
|
+
return { kind: "template", raw: value, parts };
|
|
4578
|
+
}
|
|
4579
|
+
function escapePointerSegment(segment) {
|
|
4580
|
+
return segment.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
4581
|
+
}
|
|
4582
|
+
function collectPayloadExpressions(payload, docPath = "") {
|
|
4583
|
+
const found = [];
|
|
4584
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4585
|
+
const visit = (node, pointer) => {
|
|
4586
|
+
if (typeof node === "string") {
|
|
4587
|
+
const value = parseExpressionValue(node, docPath);
|
|
4588
|
+
if (value.kind !== "literal") {
|
|
4589
|
+
found.push({ pointer, value });
|
|
4590
|
+
}
|
|
4591
|
+
return;
|
|
4592
|
+
}
|
|
4593
|
+
if (!node || typeof node !== "object") {
|
|
4594
|
+
return;
|
|
4595
|
+
}
|
|
4596
|
+
if (seen.has(node)) {
|
|
4597
|
+
return;
|
|
4598
|
+
}
|
|
4599
|
+
seen.add(node);
|
|
4600
|
+
if (Array.isArray(node)) {
|
|
4601
|
+
node.forEach((item, index) => visit(item, `${pointer}/${index}`));
|
|
4602
|
+
return;
|
|
4603
|
+
}
|
|
4604
|
+
for (const [key, value] of Object.entries(node)) {
|
|
4605
|
+
visit(value, `${pointer}/${escapePointerSegment(key)}`);
|
|
4606
|
+
}
|
|
4607
|
+
};
|
|
4608
|
+
visit(payload, "");
|
|
4609
|
+
return found;
|
|
4610
|
+
}
|
|
4611
|
+
|
|
4612
|
+
// src/arazzo.ts
|
|
4613
|
+
var yaml2 = __toESM(require("yaml"));
|
|
4614
|
+
var ID_PATTERN = /^[A-Za-z0-9_-]+$/;
|
|
4615
|
+
var OUTPUT_KEY_PATTERN = /^[a-zA-Z0-9.\-_]+$/;
|
|
4616
|
+
var VERSION_PATTERN = /^1\.0\.\d+$/;
|
|
4617
|
+
var HTTP_METHODS = ["get", "post", "put", "patch", "delete", "head", "options", "trace"];
|
|
4618
|
+
var PARAMETER_LOCATIONS = ["path", "query", "header", "cookie"];
|
|
4619
|
+
var OUTPUT_DERIVATION_MAX_DEPTH = 8;
|
|
4620
|
+
function err(message, path, extra) {
|
|
4621
|
+
throw new ArazzoError(message, { path, ...extra });
|
|
4622
|
+
}
|
|
4623
|
+
function toPlainJson(value) {
|
|
4624
|
+
try {
|
|
4625
|
+
return JSON.parse(JSON.stringify(value));
|
|
4626
|
+
} catch (error) {
|
|
4627
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
4628
|
+
throw new ArazzoError(`Arazzo document must be JSON-serializable (acyclic, bounded depth): ${message}`, {
|
|
4629
|
+
path: ""
|
|
4630
|
+
});
|
|
4631
|
+
}
|
|
4632
|
+
}
|
|
4633
|
+
function parseArazzoInput(input) {
|
|
4634
|
+
if (typeof input === "string") {
|
|
4635
|
+
let parsed;
|
|
4636
|
+
try {
|
|
4637
|
+
parsed = yaml2.parse(input);
|
|
4638
|
+
} catch (error) {
|
|
4639
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
4640
|
+
throw new ArazzoError(`Failed to parse Arazzo document: ${message}`, { path: "" });
|
|
4641
|
+
}
|
|
4642
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
4643
|
+
err("Arazzo document must be an object", "");
|
|
4644
|
+
}
|
|
4645
|
+
return toPlainJson(parsed);
|
|
4646
|
+
}
|
|
4647
|
+
if (!input || typeof input !== "object" || Array.isArray(input)) {
|
|
4648
|
+
err("Arazzo document must be an object", "");
|
|
4649
|
+
}
|
|
4650
|
+
return toPlainJson(input);
|
|
4651
|
+
}
|
|
4652
|
+
function validateCriteria(criteria, path) {
|
|
4653
|
+
if (criteria === void 0) return;
|
|
4654
|
+
if (!Array.isArray(criteria)) {
|
|
4655
|
+
err("successCriteria/criteria must be an array", path);
|
|
4656
|
+
}
|
|
4657
|
+
criteria.forEach((criterion, index) => {
|
|
4658
|
+
const cPath = `${path}/${index}`;
|
|
4659
|
+
if (!criterion || typeof criterion !== "object") {
|
|
4660
|
+
err("Criterion must be an object", cPath);
|
|
4661
|
+
}
|
|
4662
|
+
if (typeof criterion.condition !== "string" || criterion.condition === "") {
|
|
4663
|
+
err('Criterion requires a non-empty string "condition"', cPath);
|
|
4664
|
+
}
|
|
4665
|
+
const type = criterion.type;
|
|
4666
|
+
let effectiveType;
|
|
4667
|
+
if (type !== void 0) {
|
|
4668
|
+
if (typeof type === "string") {
|
|
4669
|
+
if (!["simple", "regex", "jsonpath", "xpath"].includes(type)) {
|
|
4670
|
+
err(`Unknown criterion type "${type}"`, cPath);
|
|
4671
|
+
}
|
|
4672
|
+
effectiveType = type;
|
|
4673
|
+
} else if (type && typeof type === "object") {
|
|
4674
|
+
if (type.type !== "jsonpath" && type.type !== "xpath" || typeof type.version !== "string") {
|
|
4675
|
+
err('Criterion Expression Type Object requires "type" (jsonpath|xpath) and "version"', cPath);
|
|
4676
|
+
}
|
|
4677
|
+
effectiveType = type.type;
|
|
4678
|
+
} else {
|
|
4679
|
+
err('Criterion "type" must be a string or a Criterion Expression Type Object', cPath);
|
|
4680
|
+
}
|
|
4681
|
+
}
|
|
4682
|
+
if (criterion.context !== void 0 && typeof criterion.context !== "string") {
|
|
4683
|
+
err('Criterion "context" must be a runtime expression string', cPath);
|
|
4684
|
+
}
|
|
4685
|
+
if (effectiveType !== void 0 && effectiveType !== "simple" && criterion.context === void 0) {
|
|
4686
|
+
err(`Criterion of type "${effectiveType}" requires a "context" expression`, cPath);
|
|
4687
|
+
}
|
|
4688
|
+
});
|
|
4689
|
+
}
|
|
4690
|
+
function validateActions(actions, kind, path) {
|
|
4691
|
+
if (actions === void 0) return;
|
|
4692
|
+
if (!Array.isArray(actions)) {
|
|
4693
|
+
err("Actions must be an array", path);
|
|
4694
|
+
}
|
|
4695
|
+
actions.forEach((action, index) => {
|
|
4696
|
+
const aPath = `${path}/${index}`;
|
|
4697
|
+
if (!action || typeof action !== "object") {
|
|
4698
|
+
err("Action must be an object", aPath);
|
|
4699
|
+
}
|
|
4700
|
+
if ("reference" in action) {
|
|
4701
|
+
return;
|
|
4702
|
+
}
|
|
4703
|
+
validateActionObject(action, kind, aPath);
|
|
4704
|
+
});
|
|
4705
|
+
}
|
|
4706
|
+
function validateActionObject(action, kind, aPath) {
|
|
4707
|
+
const act = action;
|
|
4708
|
+
if (typeof act.name !== "string" || act.name === "") {
|
|
4709
|
+
err('Action requires a non-empty string "name"', aPath);
|
|
4710
|
+
}
|
|
4711
|
+
const allowed = kind === "success" ? ["end", "goto"] : ["end", "retry", "goto"];
|
|
4712
|
+
if (!allowed.includes(act.type)) {
|
|
4713
|
+
err(`Invalid ${kind}-action type "${String(act.type)}" (allowed: ${allowed.join(", ")})`, aPath);
|
|
4714
|
+
}
|
|
4715
|
+
const targets = [act.workflowId, act.stepId].filter((t) => t !== void 0).length;
|
|
4716
|
+
if (act.type === "goto" && targets !== 1) {
|
|
4717
|
+
err('A "goto" action requires exactly one of "workflowId" or "stepId"', aPath);
|
|
4718
|
+
}
|
|
4719
|
+
if (act.type === "end" && targets !== 0) {
|
|
4720
|
+
err('An "end" action must not specify "workflowId" or "stepId"', aPath);
|
|
4721
|
+
}
|
|
4722
|
+
if (act.retryAfter !== void 0 && (typeof act.retryAfter !== "number" || act.retryAfter < 0)) {
|
|
4723
|
+
err('"retryAfter" must be a non-negative number', aPath);
|
|
4724
|
+
}
|
|
4725
|
+
if (act.retryLimit !== void 0 && (typeof act.retryLimit !== "number" || !Number.isInteger(act.retryLimit) || act.retryLimit < 0)) {
|
|
4726
|
+
err('"retryLimit" must be a non-negative integer', aPath);
|
|
4727
|
+
}
|
|
4728
|
+
validateCriteria(act.criteria, `${aPath}/criteria`);
|
|
4729
|
+
}
|
|
4730
|
+
function validateParameters(parameters, requireIn, path) {
|
|
4731
|
+
if (parameters === void 0) return;
|
|
4732
|
+
if (!Array.isArray(parameters)) {
|
|
4733
|
+
err("Parameters must be an array", path);
|
|
4734
|
+
}
|
|
4735
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4736
|
+
parameters.forEach((parameter, index) => {
|
|
4737
|
+
const pPath = `${path}/${index}`;
|
|
4738
|
+
if (!parameter || typeof parameter !== "object") {
|
|
4739
|
+
err("Parameter must be an object", pPath);
|
|
4740
|
+
}
|
|
4741
|
+
if ("reference" in parameter) {
|
|
4742
|
+
return;
|
|
4743
|
+
}
|
|
4744
|
+
validateParameterObject(parameter, requireIn, pPath);
|
|
4745
|
+
const param = parameter;
|
|
4746
|
+
const key = `${param.name} ${param.in ?? ""}`;
|
|
4747
|
+
if (seen.has(key)) {
|
|
4748
|
+
err(`Duplicate parameter "${param.name}"${param.in ? ` (in: ${param.in})` : ""}`, pPath);
|
|
4749
|
+
}
|
|
4750
|
+
seen.add(key);
|
|
4751
|
+
});
|
|
4752
|
+
}
|
|
4753
|
+
function validateParameterObject(param, requireIn, pPath) {
|
|
4754
|
+
if (typeof param.name !== "string" || param.name === "") {
|
|
4755
|
+
err('Parameter requires a non-empty string "name"', pPath);
|
|
4756
|
+
}
|
|
4757
|
+
const paramName = param.name;
|
|
4758
|
+
if (!("value" in param)) {
|
|
4759
|
+
err(`Parameter "${paramName}" requires a "value"`, pPath);
|
|
4760
|
+
}
|
|
4761
|
+
if (param.in !== void 0 && !PARAMETER_LOCATIONS.includes(param.in)) {
|
|
4762
|
+
err(`Invalid parameter location "${String(param.in)}"`, pPath);
|
|
4763
|
+
}
|
|
4764
|
+
if (requireIn === true && param.in === void 0) {
|
|
4765
|
+
err(`Parameter "${param.name}" on an operation step requires "in"`, pPath);
|
|
4766
|
+
}
|
|
4767
|
+
if (requireIn === false && param.in !== void 0) {
|
|
4768
|
+
err(`Parameter "${param.name}" on a workflowId step must not specify "in"`, pPath);
|
|
4769
|
+
}
|
|
4770
|
+
}
|
|
4771
|
+
function validateOutputs(outputs, path) {
|
|
4772
|
+
if (outputs === void 0) return;
|
|
4773
|
+
if (!outputs || typeof outputs !== "object" || Array.isArray(outputs)) {
|
|
4774
|
+
err('"outputs" must be an object of name \u2192 runtime expression', path);
|
|
4775
|
+
}
|
|
4776
|
+
for (const [key, value] of Object.entries(outputs)) {
|
|
4777
|
+
if (!OUTPUT_KEY_PATTERN.test(key)) {
|
|
4778
|
+
err(`Invalid output name "${key}"`, `${path}/${key}`);
|
|
4779
|
+
}
|
|
4780
|
+
if (typeof value !== "string") {
|
|
4781
|
+
err(`Output "${key}" must be a runtime expression string`, `${path}/${key}`);
|
|
4782
|
+
}
|
|
4783
|
+
}
|
|
4784
|
+
}
|
|
4785
|
+
function validateDocument(doc) {
|
|
4786
|
+
if (typeof doc.arazzo !== "string" || !VERSION_PATTERN.test(doc.arazzo)) {
|
|
4787
|
+
err(`Unsupported arazzo version "${String(doc.arazzo)}" (expected 1.0.x)`, "/arazzo");
|
|
4788
|
+
}
|
|
4789
|
+
if (!doc.info || typeof doc.info !== "object" || typeof doc.info.title !== "string" || typeof doc.info.version !== "string") {
|
|
4790
|
+
err('"info" requires string "title" and "version"', "/info");
|
|
4791
|
+
}
|
|
4792
|
+
if (!Array.isArray(doc.sourceDescriptions) || doc.sourceDescriptions.length === 0) {
|
|
4793
|
+
err('"sourceDescriptions" must be a non-empty array', "/sourceDescriptions");
|
|
4794
|
+
}
|
|
4795
|
+
const sourceNames = /* @__PURE__ */ new Set();
|
|
4796
|
+
doc.sourceDescriptions.forEach((source, index) => {
|
|
4797
|
+
const sPath = `/sourceDescriptions/${index}`;
|
|
4798
|
+
if (!source || typeof source !== "object" || typeof source.name !== "string" || !ID_PATTERN.test(source.name)) {
|
|
4799
|
+
err('Source description requires a "name" matching [A-Za-z0-9_-]+', sPath);
|
|
4800
|
+
}
|
|
4801
|
+
if (typeof source.url !== "string" || source.url === "") {
|
|
4802
|
+
err(`Source "${source.name}" requires a string "url"`, sPath);
|
|
4803
|
+
}
|
|
4804
|
+
if (source.type !== void 0 && source.type !== "openapi" && source.type !== "arazzo") {
|
|
4805
|
+
err(`Source "${source.name}" has invalid type "${String(source.type)}"`, sPath);
|
|
4806
|
+
}
|
|
4807
|
+
if (sourceNames.has(source.name)) {
|
|
4808
|
+
err(`Duplicate source description name "${source.name}"`, sPath);
|
|
4809
|
+
}
|
|
4810
|
+
sourceNames.add(source.name);
|
|
4811
|
+
});
|
|
4812
|
+
if (!Array.isArray(doc.workflows) || doc.workflows.length === 0) {
|
|
4813
|
+
err('"workflows" must be a non-empty array', "/workflows");
|
|
4814
|
+
}
|
|
4815
|
+
const workflowIds = /* @__PURE__ */ new Set();
|
|
4816
|
+
doc.workflows.forEach((workflow, wIndex) => {
|
|
4817
|
+
const wPath = `/workflows/${wIndex}`;
|
|
4818
|
+
if (!workflow || typeof workflow !== "object" || typeof workflow.workflowId !== "string" || !ID_PATTERN.test(workflow.workflowId)) {
|
|
4819
|
+
err('Workflow requires a "workflowId" matching [A-Za-z0-9_-]+', wPath);
|
|
4820
|
+
}
|
|
4821
|
+
if (workflowIds.has(workflow.workflowId)) {
|
|
4822
|
+
err(`Duplicate workflowId "${workflow.workflowId}"`, wPath);
|
|
4823
|
+
}
|
|
4824
|
+
workflowIds.add(workflow.workflowId);
|
|
4825
|
+
if (!Array.isArray(workflow.steps) || workflow.steps.length === 0) {
|
|
4826
|
+
err(`Workflow "${workflow.workflowId}" requires a non-empty "steps" array`, `${wPath}/steps`);
|
|
4827
|
+
}
|
|
4828
|
+
validateParameters(workflow.parameters, void 0, `${wPath}/parameters`);
|
|
4829
|
+
validateActions(workflow.successActions, "success", `${wPath}/successActions`);
|
|
4830
|
+
validateActions(workflow.failureActions, "failure", `${wPath}/failureActions`);
|
|
4831
|
+
validateOutputs(workflow.outputs, `${wPath}/outputs`);
|
|
4832
|
+
const stepIds = /* @__PURE__ */ new Set();
|
|
4833
|
+
workflow.steps.forEach((step, sIndex) => {
|
|
4834
|
+
const sPath = `${wPath}/steps/${sIndex}`;
|
|
4835
|
+
if (!step || typeof step !== "object" || typeof step.stepId !== "string" || !ID_PATTERN.test(step.stepId)) {
|
|
4836
|
+
err('Step requires a "stepId" matching [A-Za-z0-9_-]+', sPath);
|
|
4837
|
+
}
|
|
4838
|
+
if (stepIds.has(step.stepId)) {
|
|
4839
|
+
err(`Duplicate stepId "${step.stepId}" in workflow "${workflow.workflowId}"`, sPath);
|
|
4840
|
+
}
|
|
4841
|
+
stepIds.add(step.stepId);
|
|
4842
|
+
const kinds = [step.operationId, step.operationPath, step.workflowId].filter((k) => k !== void 0).length;
|
|
4843
|
+
if (kinds !== 1) {
|
|
4844
|
+
err(`Step "${step.stepId}" requires exactly one of "operationId", "operationPath", or "workflowId"`, sPath);
|
|
4845
|
+
}
|
|
4846
|
+
validateParameters(step.parameters, step.workflowId !== void 0 ? false : true, `${sPath}/parameters`);
|
|
4847
|
+
validateCriteria(step.successCriteria, `${sPath}/successCriteria`);
|
|
4848
|
+
validateActions(step.onSuccess, "success", `${sPath}/onSuccess`);
|
|
4849
|
+
validateActions(step.onFailure, "failure", `${sPath}/onFailure`);
|
|
4850
|
+
validateOutputs(step.outputs, `${sPath}/outputs`);
|
|
4851
|
+
});
|
|
4852
|
+
});
|
|
4853
|
+
}
|
|
4854
|
+
function ownComponent(group, name) {
|
|
4855
|
+
if (!group || !Object.prototype.hasOwnProperty.call(group, name)) {
|
|
4856
|
+
return void 0;
|
|
4857
|
+
}
|
|
4858
|
+
const value = group[name];
|
|
4859
|
+
return value !== null && typeof value === "object" ? value : void 0;
|
|
4860
|
+
}
|
|
4861
|
+
function resolveReusable(entry, components, expectedGroup, path) {
|
|
4862
|
+
if (!entry || typeof entry !== "object" || !("reference" in entry)) {
|
|
4863
|
+
return entry;
|
|
4864
|
+
}
|
|
4865
|
+
const reusable = entry;
|
|
4866
|
+
if (typeof reusable.reference !== "string") {
|
|
4867
|
+
err('Reusable Object "reference" must be a string', path);
|
|
4868
|
+
}
|
|
4869
|
+
const ast = parseRuntimeExpression(reusable.reference, path);
|
|
4870
|
+
if (ast.type !== "components" || ast.path.length < 2 || ast.path[0] !== expectedGroup) {
|
|
4871
|
+
err(`Reference "${reusable.reference}" must point at $components.${expectedGroup}.<name>`, path);
|
|
4872
|
+
}
|
|
4873
|
+
const name = ast.path.slice(1).join(".");
|
|
4874
|
+
const target = ownComponent(components?.[expectedGroup], name);
|
|
4875
|
+
if (!target) {
|
|
4876
|
+
err(`Unknown reference "$components.${expectedGroup}.${name}"`, path);
|
|
4877
|
+
}
|
|
4878
|
+
const resolved = JSON.parse(JSON.stringify(target));
|
|
4879
|
+
if (expectedGroup === "parameters" && "value" in reusable) {
|
|
4880
|
+
resolved.value = reusable.value;
|
|
4881
|
+
}
|
|
4882
|
+
return resolved;
|
|
4883
|
+
}
|
|
4884
|
+
function resolveInputRefs(node, components, path, seen) {
|
|
4885
|
+
if (Array.isArray(node)) {
|
|
4886
|
+
return node.map((item) => resolveInputRefs(item, components, path, seen));
|
|
4887
|
+
}
|
|
4888
|
+
if (!node || typeof node !== "object") {
|
|
4889
|
+
return node;
|
|
4890
|
+
}
|
|
4891
|
+
const record = node;
|
|
4892
|
+
const ref = record["$ref"];
|
|
4893
|
+
if (typeof ref === "string") {
|
|
4894
|
+
const prefix = "#/components/inputs/";
|
|
4895
|
+
if (!ref.startsWith(prefix)) {
|
|
4896
|
+
err(`Unsupported $ref "${ref}" in workflow inputs (only ${prefix}<name> is resolvable)`, path);
|
|
4897
|
+
}
|
|
4898
|
+
const name = ref.slice(prefix.length);
|
|
4899
|
+
const target = ownComponent(components?.inputs, name);
|
|
4900
|
+
if (!target) {
|
|
4901
|
+
err(`Unknown workflow inputs reference "${ref}"`, path);
|
|
4902
|
+
}
|
|
4903
|
+
if (seen.has(name)) {
|
|
4904
|
+
err(`Cyclic workflow inputs reference "${ref}"`, path);
|
|
4905
|
+
}
|
|
4906
|
+
seen.add(name);
|
|
4907
|
+
const resolved = resolveInputRefs(target, components, path, seen);
|
|
4908
|
+
seen.delete(name);
|
|
4909
|
+
return resolved;
|
|
4910
|
+
}
|
|
4911
|
+
const out = {};
|
|
4912
|
+
for (const [key, value] of Object.entries(record)) {
|
|
4913
|
+
out[key] = resolveInputRefs(value, components, path, seen);
|
|
4914
|
+
}
|
|
4915
|
+
return out;
|
|
4916
|
+
}
|
|
4917
|
+
async function prepareSources(doc, options) {
|
|
4918
|
+
const declared = new Map(doc.sourceDescriptions.map((s) => [s.name, s]));
|
|
4919
|
+
const generators = /* @__PURE__ */ new Map();
|
|
4920
|
+
const sourceTypes = /* @__PURE__ */ new Map();
|
|
4921
|
+
for (const [name, source] of Object.entries(options.sources ?? {})) {
|
|
4922
|
+
if (!declared.has(name)) {
|
|
4923
|
+
err(`options.sources contains "${name}", which is not a declared source description`, "/sourceDescriptions", {
|
|
4924
|
+
declared: [...declared.keys()]
|
|
4925
|
+
});
|
|
4926
|
+
}
|
|
4927
|
+
if (source instanceof OpenAPIToolGenerator) {
|
|
4928
|
+
generators.set(name, source);
|
|
4929
|
+
} else {
|
|
4930
|
+
generators.set(name, await OpenAPIToolGenerator.fromJSON(source, options.loadOptions));
|
|
4931
|
+
}
|
|
4932
|
+
}
|
|
4933
|
+
for (const [name, source] of declared) {
|
|
4934
|
+
sourceTypes.set(name, source.type ?? "openapi");
|
|
4935
|
+
}
|
|
4936
|
+
const operationIndex = /* @__PURE__ */ new Map();
|
|
4937
|
+
for (const [name, generator] of generators) {
|
|
4938
|
+
const document = generator.getDocument();
|
|
4939
|
+
for (const [pathStr, pathItem] of Object.entries(document.paths ?? {})) {
|
|
4940
|
+
if (!pathItem || typeof pathItem !== "object") continue;
|
|
4941
|
+
for (const method of HTTP_METHODS) {
|
|
4942
|
+
const operation = pathItem[method];
|
|
4943
|
+
if (!operation || typeof operation !== "object") continue;
|
|
4944
|
+
const operationId = operation["operationId"];
|
|
4945
|
+
if (typeof operationId !== "string") continue;
|
|
4946
|
+
const hits = operationIndex.get(operationId) ?? [];
|
|
4947
|
+
hits.push({ source: name, path: pathStr, method });
|
|
4948
|
+
operationIndex.set(operationId, hits);
|
|
4949
|
+
}
|
|
4950
|
+
}
|
|
4951
|
+
}
|
|
4952
|
+
return { generators, operationIndex, sourceTypes };
|
|
4953
|
+
}
|
|
4954
|
+
function requireGenerator(ctx, source, path) {
|
|
4955
|
+
if (ctx.sourceTypes.get(source) === "arazzo") {
|
|
4956
|
+
err(`Source "${source}" has type "arazzo" \u2014 nested Arazzo sources are not supported`, path);
|
|
4957
|
+
}
|
|
4958
|
+
const generator = ctx.generators.get(source);
|
|
4959
|
+
if (!generator) {
|
|
4960
|
+
err(`No document supplied for source "${source}" (add it to options.sources)`, path, {
|
|
4961
|
+
supplied: [...ctx.generators.keys()]
|
|
4962
|
+
});
|
|
4963
|
+
}
|
|
4964
|
+
return generator;
|
|
4965
|
+
}
|
|
4966
|
+
function parseOperationPath(value, path) {
|
|
4967
|
+
if (!value.startsWith("{")) {
|
|
4968
|
+
err(`operationPath "${value}" must start with a "{$sourceDescriptions...}" expression`, path);
|
|
4969
|
+
}
|
|
4970
|
+
const close = value.indexOf("}");
|
|
4971
|
+
if (close === -1) {
|
|
4972
|
+
err(`operationPath "${value}" is missing "}"`, path);
|
|
4973
|
+
}
|
|
4974
|
+
const ast = parseRuntimeExpression(value.slice(1, close), path);
|
|
4975
|
+
if (ast.type !== "sourceDescriptions" || ast.path.length !== 2 || ast.path[1] !== "url") {
|
|
4976
|
+
err(`operationPath "${value}" must reference $sourceDescriptions.<name>.url`, path);
|
|
4977
|
+
}
|
|
4978
|
+
const source = ast.path[0];
|
|
4979
|
+
const rest = value.slice(close + 1);
|
|
4980
|
+
if (!rest.startsWith("#/")) {
|
|
4981
|
+
err(`operationPath "${value}" requires a "#/paths/..." JSON Pointer after the source expression`, path);
|
|
4982
|
+
}
|
|
4983
|
+
const segments = rest.slice(2).split("/").map((segment) => segment.replace(/~1/g, "/").replace(/~0/g, "~"));
|
|
4984
|
+
if (segments.length !== 3 || segments[0] !== "paths") {
|
|
4985
|
+
err(`operationPath pointer in "${value}" must have the shape #/paths/<path>/<method>`, path);
|
|
4986
|
+
}
|
|
4987
|
+
const method = segments[2].toLowerCase();
|
|
4988
|
+
if (!HTTP_METHODS.includes(method)) {
|
|
4989
|
+
err(`operationPath "${value}" ends in unknown HTTP method "${segments[2]}"`, path);
|
|
4990
|
+
}
|
|
4991
|
+
return { source, path: segments[1], method };
|
|
4992
|
+
}
|
|
4993
|
+
function resolveOperationRef(step, ctx, path) {
|
|
4994
|
+
if (step.operationPath !== void 0) {
|
|
4995
|
+
return parseOperationPath(step.operationPath, path);
|
|
4996
|
+
}
|
|
4997
|
+
const ref = step.operationId;
|
|
4998
|
+
if (ref.startsWith("$")) {
|
|
4999
|
+
const ast = parseRuntimeExpression(ref, path);
|
|
5000
|
+
if (ast.type !== "sourceDescriptions" || ast.path.length < 2) {
|
|
5001
|
+
err(`operationId expression "${ref}" must be $sourceDescriptions.<name>.<operationId>`, path);
|
|
5002
|
+
}
|
|
5003
|
+
const source = ast.path[0];
|
|
5004
|
+
const operationId = ast.path.slice(1).join(".");
|
|
5005
|
+
const hits2 = (ctx.operationIndex.get(operationId) ?? []).filter((h) => h.source === source);
|
|
5006
|
+
if (hits2.length === 0) {
|
|
5007
|
+
requireGenerator(ctx, source, path);
|
|
5008
|
+
err(`operationId "${operationId}" not found in source "${source}"`, path);
|
|
5009
|
+
}
|
|
5010
|
+
if (hits2.length > 1) {
|
|
5011
|
+
err(`operationId "${operationId}" is duplicated inside source "${source}"`, path, { hits: hits2 });
|
|
5012
|
+
}
|
|
5013
|
+
return { ...hits2[0], operationId };
|
|
5014
|
+
}
|
|
5015
|
+
const hits = ctx.operationIndex.get(ref) ?? [];
|
|
5016
|
+
if (hits.length === 0) {
|
|
5017
|
+
err(`operationId "${ref}" not found in any supplied source (${[...ctx.generators.keys()].join(", ") || "none"})`, path);
|
|
5018
|
+
}
|
|
5019
|
+
if (hits.length > 1) {
|
|
5020
|
+
err(
|
|
5021
|
+
`operationId "${ref}" is ambiguous across sources (${hits.map((h) => h.source).join(", ")}) \u2014 pin it with $sourceDescriptions.<name>.${ref}`,
|
|
5022
|
+
path,
|
|
5023
|
+
{ hits }
|
|
5024
|
+
);
|
|
5025
|
+
}
|
|
5026
|
+
return { ...hits[0], operationId: ref };
|
|
5027
|
+
}
|
|
5028
|
+
function checkCycles(edges, kind) {
|
|
5029
|
+
const state = /* @__PURE__ */ new Map();
|
|
5030
|
+
for (const start of edges.keys()) {
|
|
5031
|
+
if (state.get(start) === "done") continue;
|
|
5032
|
+
const stack = [{ node: start, next: 0 }];
|
|
5033
|
+
state.set(start, "visiting");
|
|
5034
|
+
while (stack.length > 0) {
|
|
5035
|
+
const frame = stack[stack.length - 1];
|
|
5036
|
+
const targets = edges.get(frame.node) ?? [];
|
|
5037
|
+
if (frame.next >= targets.length) {
|
|
5038
|
+
state.set(frame.node, "done");
|
|
5039
|
+
stack.pop();
|
|
5040
|
+
continue;
|
|
5041
|
+
}
|
|
5042
|
+
const target = targets[frame.next++];
|
|
5043
|
+
const targetState = state.get(target);
|
|
5044
|
+
if (targetState === "visiting") {
|
|
5045
|
+
const cycle = [...stack.map((f) => f.node), target];
|
|
5046
|
+
err(`Cyclic ${kind}: ${cycle.slice(cycle.indexOf(target)).join(" -> ")}`, "/workflows");
|
|
5047
|
+
}
|
|
5048
|
+
if (targetState !== "done") {
|
|
5049
|
+
state.set(target, "visiting");
|
|
5050
|
+
stack.push({ node: target, next: 0 });
|
|
5051
|
+
}
|
|
5052
|
+
}
|
|
5053
|
+
}
|
|
5054
|
+
}
|
|
5055
|
+
function toCriterionIR(criterion, path) {
|
|
5056
|
+
const ir = {
|
|
5057
|
+
condition: criterion.condition,
|
|
5058
|
+
type: "simple"
|
|
5059
|
+
};
|
|
5060
|
+
if (criterion.context !== void 0) {
|
|
5061
|
+
ir.context = parseRuntimeExpression(criterion.context, path);
|
|
5062
|
+
}
|
|
5063
|
+
if (typeof criterion.type === "string") {
|
|
5064
|
+
ir.type = criterion.type;
|
|
5065
|
+
} else if (criterion.type) {
|
|
5066
|
+
ir.type = criterion.type.type;
|
|
5067
|
+
ir.version = criterion.type.version;
|
|
5068
|
+
}
|
|
5069
|
+
return ir;
|
|
5070
|
+
}
|
|
5071
|
+
function toActionIR(action, kind, path) {
|
|
5072
|
+
const failure = action;
|
|
5073
|
+
return {
|
|
5074
|
+
name: action.name,
|
|
5075
|
+
kind,
|
|
5076
|
+
type: action.type,
|
|
5077
|
+
...action.workflowId !== void 0 && { workflowId: action.workflowId },
|
|
5078
|
+
...action.stepId !== void 0 && { stepId: action.stepId },
|
|
5079
|
+
...failure.retryAfter !== void 0 && { retryAfter: failure.retryAfter },
|
|
5080
|
+
...failure.retryLimit !== void 0 && { retryLimit: failure.retryLimit },
|
|
5081
|
+
...action.criteria && { criteria: action.criteria.map((c, i) => toCriterionIR(c, `${path}/criteria/${i}`)) }
|
|
5082
|
+
};
|
|
5083
|
+
}
|
|
5084
|
+
function resolveActions(actions, kind, components, path) {
|
|
5085
|
+
const group = kind === "success" ? "successActions" : "failureActions";
|
|
5086
|
+
return actions.map((action, index) => {
|
|
5087
|
+
const aPath = `${path}/${index}`;
|
|
5088
|
+
const concrete = resolveReusable(action, components, group, aPath);
|
|
5089
|
+
validateActionObject(concrete, kind, aPath);
|
|
5090
|
+
return toActionIR(concrete, kind, aPath);
|
|
5091
|
+
});
|
|
5092
|
+
}
|
|
5093
|
+
function resolveParameters(parameters, components, requireIn, path) {
|
|
5094
|
+
const seen = /* @__PURE__ */ new Set();
|
|
5095
|
+
return parameters.map((parameter, index) => {
|
|
5096
|
+
const pPath = `${path}/${index}`;
|
|
5097
|
+
const concrete = resolveReusable(parameter, components, "parameters", pPath);
|
|
5098
|
+
validateParameterObject(concrete, requireIn, pPath);
|
|
5099
|
+
const key = `${concrete.name} ${concrete.in ?? ""}`;
|
|
5100
|
+
if (seen.has(key)) {
|
|
5101
|
+
err(`Duplicate parameter "${concrete.name}"${concrete.in ? ` (in: ${concrete.in})` : ""}`, pPath);
|
|
5102
|
+
}
|
|
5103
|
+
seen.add(key);
|
|
5104
|
+
return {
|
|
5105
|
+
name: concrete.name,
|
|
5106
|
+
...concrete.in !== void 0 && { in: concrete.in },
|
|
5107
|
+
value: parseExpressionValue(concrete.value, pPath)
|
|
5108
|
+
};
|
|
5109
|
+
});
|
|
5110
|
+
}
|
|
5111
|
+
function parseOutputs(outputs, path) {
|
|
5112
|
+
if (!outputs) return void 0;
|
|
5113
|
+
const parsed = {};
|
|
5114
|
+
for (const [name, expression] of Object.entries(outputs)) {
|
|
5115
|
+
parsed[name] = parseRuntimeExpression(expression, `${path}/${name}`);
|
|
5116
|
+
}
|
|
5117
|
+
return parsed;
|
|
5118
|
+
}
|
|
5119
|
+
async function resolveStepOperation(ref, ctx, docPath) {
|
|
5120
|
+
const key = `${ref.source} ${ref.method} ${ref.path}`;
|
|
5121
|
+
let cached = ctx.operationCache.get(key);
|
|
5122
|
+
if (!cached) {
|
|
5123
|
+
const generator = requireGenerator(ctx.sources, ref.source, docPath);
|
|
5124
|
+
cached = generator.generateTool(ref.path, ref.method, ctx.generateOptions).catch((error) => {
|
|
5125
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
5126
|
+
throw new ArazzoError(
|
|
5127
|
+
`Failed to resolve ${ref.method.toUpperCase()} ${ref.path} from source "${ref.source}": ${message}`,
|
|
5128
|
+
{ path: docPath, source: ref.source }
|
|
5129
|
+
);
|
|
5130
|
+
});
|
|
5131
|
+
ctx.operationCache.set(key, cached);
|
|
5132
|
+
}
|
|
5133
|
+
return cached;
|
|
5134
|
+
}
|
|
5135
|
+
async function buildStepIR(step, ctx, path) {
|
|
5136
|
+
const components = ctx.doc.components;
|
|
5137
|
+
const base = {
|
|
5138
|
+
stepId: step.stepId,
|
|
5139
|
+
...step.description !== void 0 && { description: step.description },
|
|
5140
|
+
...step.parameters && {
|
|
5141
|
+
parameters: resolveParameters(
|
|
5142
|
+
step.parameters,
|
|
5143
|
+
components,
|
|
5144
|
+
step.workflowId !== void 0 ? false : true,
|
|
5145
|
+
`${path}/parameters`
|
|
5146
|
+
)
|
|
5147
|
+
},
|
|
5148
|
+
...step.successCriteria && {
|
|
5149
|
+
successCriteria: step.successCriteria.map((c, i) => toCriterionIR(c, `${path}/successCriteria/${i}`))
|
|
5150
|
+
},
|
|
5151
|
+
...step.onSuccess && { onSuccess: resolveActions(step.onSuccess, "success", components, `${path}/onSuccess`) },
|
|
5152
|
+
...step.onFailure && { onFailure: resolveActions(step.onFailure, "failure", components, `${path}/onFailure`) },
|
|
5153
|
+
...step.outputs && { outputs: parseOutputs(step.outputs, `${path}/outputs`) }
|
|
5154
|
+
};
|
|
5155
|
+
if (step.workflowId !== void 0) {
|
|
5156
|
+
if (step.requestBody !== void 0) {
|
|
5157
|
+
err(`Step "${step.stepId}" invokes a workflow and must not declare a requestBody`, `${path}/requestBody`);
|
|
5158
|
+
}
|
|
5159
|
+
if (step.workflowId.startsWith("$")) {
|
|
5160
|
+
err(`Step "${step.stepId}" invokes a workflow in another Arazzo document \u2014 nested Arazzo sources are not supported`, path);
|
|
5161
|
+
}
|
|
5162
|
+
if (!ctx.workflowIds.has(step.workflowId)) {
|
|
5163
|
+
err(`Step "${step.stepId}" references unknown workflow "${step.workflowId}"`, path);
|
|
5164
|
+
}
|
|
5165
|
+
const ir2 = { kind: "workflow", workflowId: step.workflowId, ...base };
|
|
5166
|
+
return ir2;
|
|
5167
|
+
}
|
|
5168
|
+
const ref = resolveOperationRef(step, ctx.sources, path);
|
|
5169
|
+
const tool = await resolveStepOperation(ref, ctx, path);
|
|
5170
|
+
const operation = {
|
|
5171
|
+
inputSchema: tool.inputSchema,
|
|
5172
|
+
outputSchema: tool.outputSchema,
|
|
5173
|
+
mapper: tool.mapper,
|
|
5174
|
+
...tool.metadata.security && { security: tool.metadata.security },
|
|
5175
|
+
...tool.metadata.servers && { servers: tool.metadata.servers }
|
|
5176
|
+
};
|
|
5177
|
+
let requestBody;
|
|
5178
|
+
if (step.requestBody !== void 0) {
|
|
5179
|
+
if (!step.requestBody || typeof step.requestBody !== "object") {
|
|
5180
|
+
err(`Step "${step.stepId}" requestBody must be an object`, `${path}/requestBody`);
|
|
5181
|
+
}
|
|
5182
|
+
requestBody = {
|
|
5183
|
+
...step.requestBody.contentType !== void 0 && { contentType: step.requestBody.contentType },
|
|
5184
|
+
...step.requestBody.payload !== void 0 && { payload: step.requestBody.payload }
|
|
5185
|
+
};
|
|
5186
|
+
const expressions = collectPayloadExpressions(step.requestBody.payload, `${path}/requestBody/payload`);
|
|
5187
|
+
if (expressions.length > 0) {
|
|
5188
|
+
requestBody.payloadExpressions = expressions;
|
|
5189
|
+
}
|
|
5190
|
+
if (step.requestBody.replacements !== void 0) {
|
|
5191
|
+
if (!Array.isArray(step.requestBody.replacements)) {
|
|
5192
|
+
err(`Step "${step.stepId}" requestBody.replacements must be an array`, `${path}/requestBody/replacements`);
|
|
5193
|
+
}
|
|
5194
|
+
requestBody.replacements = step.requestBody.replacements.map((replacement, index) => {
|
|
5195
|
+
const rPath = `${path}/requestBody/replacements/${index}`;
|
|
5196
|
+
if (!replacement || typeof replacement !== "object" || typeof replacement.target !== "string") {
|
|
5197
|
+
err('Replacement requires a string "target"', rPath);
|
|
5198
|
+
}
|
|
5199
|
+
return { target: replacement.target, value: parseExpressionValue(replacement.value, rPath) };
|
|
5200
|
+
});
|
|
5201
|
+
}
|
|
5202
|
+
}
|
|
5203
|
+
const ir = {
|
|
5204
|
+
kind: "operation",
|
|
5205
|
+
source: ref.source,
|
|
5206
|
+
path: ref.path,
|
|
5207
|
+
method: ref.method,
|
|
5208
|
+
...ref.operationId !== void 0 && { operationId: ref.operationId },
|
|
5209
|
+
operation,
|
|
5210
|
+
...requestBody && { requestBody },
|
|
5211
|
+
...base
|
|
5212
|
+
};
|
|
5213
|
+
return ir;
|
|
5214
|
+
}
|
|
5215
|
+
function isRecord(value) {
|
|
5216
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5217
|
+
}
|
|
5218
|
+
function walkPointer(schema, pointer) {
|
|
5219
|
+
if (pointer === void 0 || pointer === "") {
|
|
5220
|
+
return schema;
|
|
5221
|
+
}
|
|
5222
|
+
let node = schema;
|
|
5223
|
+
for (const rawSegment of pointer.slice(1).split("/")) {
|
|
5224
|
+
const segment = rawSegment.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
5225
|
+
if (!isRecord(node)) return void 0;
|
|
5226
|
+
const properties = node["properties"];
|
|
5227
|
+
if (isRecord(properties) && properties[segment] !== void 0) {
|
|
5228
|
+
node = properties[segment];
|
|
5229
|
+
continue;
|
|
5230
|
+
}
|
|
5231
|
+
if (/^\d+$/.test(segment) && node["items"] !== void 0 && !Array.isArray(node["items"])) {
|
|
5232
|
+
node = node["items"];
|
|
5233
|
+
continue;
|
|
5234
|
+
}
|
|
5235
|
+
return void 0;
|
|
5236
|
+
}
|
|
5237
|
+
return node;
|
|
5238
|
+
}
|
|
5239
|
+
function primaryResponseSchema(outputSchema) {
|
|
5240
|
+
if (isRecord(outputSchema) && Array.isArray(outputSchema["oneOf"])) {
|
|
5241
|
+
const variants = outputSchema["oneOf"];
|
|
5242
|
+
if (variants.length > 0 && variants.every((v) => isRecord(v) && v["x-status-code"] !== void 0)) {
|
|
5243
|
+
return variants[0];
|
|
5244
|
+
}
|
|
5245
|
+
}
|
|
5246
|
+
return outputSchema;
|
|
5247
|
+
}
|
|
5248
|
+
function deriveOutputSchema(ast, steps, inputSchema, depth, stepContext) {
|
|
5249
|
+
if (depth >= OUTPUT_DERIVATION_MAX_DEPTH) {
|
|
5250
|
+
return {};
|
|
5251
|
+
}
|
|
5252
|
+
if (ast.type === "statusCode") {
|
|
5253
|
+
return { type: "number" };
|
|
5254
|
+
}
|
|
5255
|
+
if (ast.type === "url" || ast.type === "method") {
|
|
5256
|
+
return { type: "string" };
|
|
5257
|
+
}
|
|
5258
|
+
if (ast.type === "response") {
|
|
5259
|
+
if (ast.source !== "body") {
|
|
5260
|
+
return { type: "string" };
|
|
5261
|
+
}
|
|
5262
|
+
if (!stepContext) {
|
|
5263
|
+
return {};
|
|
5264
|
+
}
|
|
5265
|
+
const body = primaryResponseSchema(stepContext.operation.outputSchema);
|
|
5266
|
+
const target = walkPointer(body, ast.pointer);
|
|
5267
|
+
return isRecord(target) ? target : {};
|
|
5268
|
+
}
|
|
5269
|
+
if (ast.type === "inputs") {
|
|
5270
|
+
const properties = isRecord(inputSchema) ? inputSchema["properties"] : void 0;
|
|
5271
|
+
const target = isRecord(properties) ? properties[ast.path.join(".")] : void 0;
|
|
5272
|
+
return isRecord(target) ? target : {};
|
|
5273
|
+
}
|
|
5274
|
+
if (ast.type === "steps" && ast.path.length >= 3 && ast.path[1] === "outputs") {
|
|
5275
|
+
const step = steps.get(ast.path[0]);
|
|
5276
|
+
if (step?.kind === "operation") {
|
|
5277
|
+
const stepOutput = step.outputs?.[ast.path.slice(2).join(".")];
|
|
5278
|
+
if (stepOutput) {
|
|
5279
|
+
return deriveOutputSchema(stepOutput, steps, inputSchema, depth + 1, step);
|
|
5280
|
+
}
|
|
5281
|
+
}
|
|
5282
|
+
return {};
|
|
5283
|
+
}
|
|
5284
|
+
return {};
|
|
5285
|
+
}
|
|
5286
|
+
function deriveOutputsSchema(outputs, steps, inputSchema) {
|
|
5287
|
+
if (!outputs) {
|
|
5288
|
+
return void 0;
|
|
5289
|
+
}
|
|
5290
|
+
const stepMap = new Map(steps.map((s) => [s.stepId, s]));
|
|
5291
|
+
const properties = {};
|
|
5292
|
+
for (const [name, ast] of Object.entries(outputs)) {
|
|
5293
|
+
const derived = deriveOutputSchema(ast, stepMap, inputSchema, 0);
|
|
5294
|
+
const copied = JSON.parse(JSON.stringify(derived));
|
|
5295
|
+
properties[name] = { ...copied, description: `Arazzo output: ${ast.raw}` };
|
|
5296
|
+
}
|
|
5297
|
+
return { type: "object", properties };
|
|
5298
|
+
}
|
|
5299
|
+
function applySchemaPipeline(schema, options, isInputRoot) {
|
|
5300
|
+
const formatResolvers = {
|
|
5301
|
+
...options.resolveFormats ? BUILTIN_FORMAT_RESOLVERS : {},
|
|
5302
|
+
...options.formatResolvers
|
|
5303
|
+
};
|
|
5304
|
+
let resolved = Object.keys(formatResolvers).length > 0 ? resolveSchemaFormats(schema, formatResolvers) : schema;
|
|
5305
|
+
resolved = SchemaBuilder.truncateDepth(resolved, Math.max(1, options.maxSchemaDepth ?? 10));
|
|
5306
|
+
if (options.stripExamples) resolved = SchemaBuilder.stripExamples(resolved);
|
|
5307
|
+
if (options.maxDescriptionLength !== void 0) {
|
|
5308
|
+
resolved = SchemaBuilder.capDescriptions(resolved, options.maxDescriptionLength);
|
|
5309
|
+
}
|
|
5310
|
+
if (options.maxProperties !== void 0) {
|
|
5311
|
+
if (isInputRoot) {
|
|
5312
|
+
const properties = resolved.properties;
|
|
5313
|
+
if (properties && typeof properties === "object") {
|
|
5314
|
+
const limited = {};
|
|
5315
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
5316
|
+
limited[key] = SchemaBuilder.limitProperties(value, options.maxProperties);
|
|
5317
|
+
}
|
|
5318
|
+
resolved = { ...resolved, properties: limited };
|
|
5319
|
+
}
|
|
5320
|
+
} else {
|
|
5321
|
+
resolved = SchemaBuilder.limitProperties(resolved, options.maxProperties);
|
|
5322
|
+
}
|
|
5323
|
+
}
|
|
5324
|
+
if (options.target) {
|
|
5325
|
+
resolved = applyClientTarget(resolved, options.target);
|
|
5326
|
+
}
|
|
5327
|
+
return resolved;
|
|
5328
|
+
}
|
|
5329
|
+
function buildWorkflowTool(workflow, stepIRs, ctx, wPath) {
|
|
5330
|
+
const options = ctx.generateOptions;
|
|
5331
|
+
let inputSchema;
|
|
5332
|
+
let rawInputSchema;
|
|
5333
|
+
if (workflow.inputs !== void 0) {
|
|
5334
|
+
const resolved = resolveInputRefs(workflow.inputs, ctx.doc.components, `${wPath}/inputs`, /* @__PURE__ */ new Set());
|
|
5335
|
+
rawInputSchema = toJsonSchema(resolved);
|
|
5336
|
+
inputSchema = applySchemaPipeline(rawInputSchema, options, true);
|
|
5337
|
+
} else {
|
|
5338
|
+
inputSchema = { type: "object", properties: {} };
|
|
5339
|
+
}
|
|
5340
|
+
const derivedOutput = deriveOutputsSchema(parseOutputs(workflow.outputs, `${wPath}/outputs`), stepIRs, rawInputSchema);
|
|
5341
|
+
const outputSchema = derivedOutput ? applySchemaPipeline(derivedOutput, options, false) : void 0;
|
|
5342
|
+
const name = normalizeToolName(workflow.workflowId, options.maxToolNameLength ?? 64, workflow.workflowId);
|
|
5343
|
+
const description = workflow.summary && workflow.description ? `${workflow.summary}
|
|
5344
|
+
|
|
5345
|
+
${workflow.description}` : workflow.summary ?? workflow.description ?? `Arazzo workflow: ${workflow.workflowId}`;
|
|
5346
|
+
const operationSteps = stepIRs.filter((s) => s.kind === "operation");
|
|
5347
|
+
const allReadOnly = operationSteps.length === stepIRs.length && operationSteps.every((s) => inferAnnotationsFromMethod(s.method).readOnlyHint === true);
|
|
5348
|
+
const security = [];
|
|
5349
|
+
const seenSecurity = /* @__PURE__ */ new Set();
|
|
5350
|
+
for (const step of operationSteps) {
|
|
5351
|
+
for (const requirement of step.operation.security ?? []) {
|
|
5352
|
+
const key = JSON.stringify(requirement);
|
|
5353
|
+
if (!seenSecurity.has(key)) {
|
|
5354
|
+
seenSecurity.add(key);
|
|
5355
|
+
security.push(requirement);
|
|
5356
|
+
}
|
|
5357
|
+
}
|
|
5358
|
+
}
|
|
5359
|
+
const ir = {
|
|
5360
|
+
arazzoVersion: ctx.doc.arazzo,
|
|
5361
|
+
workflowId: workflow.workflowId,
|
|
5362
|
+
...workflow.summary !== void 0 && { summary: workflow.summary },
|
|
5363
|
+
...workflow.description !== void 0 && { description: workflow.description },
|
|
5364
|
+
...rawInputSchema !== void 0 && { inputSchema: rawInputSchema },
|
|
5365
|
+
...workflow.dependsOn && { dependsOn: workflow.dependsOn },
|
|
5366
|
+
...workflow.parameters && {
|
|
5367
|
+
parameters: resolveParameters(workflow.parameters, ctx.doc.components, void 0, `${wPath}/parameters`)
|
|
5368
|
+
},
|
|
5369
|
+
steps: stepIRs,
|
|
5370
|
+
...workflow.successActions && {
|
|
5371
|
+
successActions: resolveActions(workflow.successActions, "success", ctx.doc.components, `${wPath}/successActions`)
|
|
5372
|
+
},
|
|
5373
|
+
...workflow.failureActions && {
|
|
5374
|
+
failureActions: resolveActions(workflow.failureActions, "failure", ctx.doc.components, `${wPath}/failureActions`)
|
|
5375
|
+
},
|
|
5376
|
+
...workflow.outputs && { outputs: parseOutputs(workflow.outputs, `${wPath}/outputs`) }
|
|
5377
|
+
};
|
|
5378
|
+
const tool = {
|
|
5379
|
+
name,
|
|
5380
|
+
...workflow.summary !== void 0 && { title: workflow.summary },
|
|
5381
|
+
description,
|
|
5382
|
+
...allReadOnly && {
|
|
5383
|
+
annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false }
|
|
5384
|
+
},
|
|
5385
|
+
inputSchema,
|
|
5386
|
+
outputSchema,
|
|
5387
|
+
// A workflow tool has no single HTTP shape — each step's mapper lives at
|
|
5388
|
+
// metadata.workflow.steps[*].operation.mapper
|
|
5389
|
+
mapper: [],
|
|
5390
|
+
metadata: {
|
|
5391
|
+
path: `arazzo:${workflow.workflowId}`,
|
|
5392
|
+
method: "post",
|
|
5393
|
+
operationId: workflow.workflowId,
|
|
5394
|
+
...workflow.summary !== void 0 && { operationSummary: workflow.summary },
|
|
5395
|
+
...workflow.description !== void 0 && { operationDescription: workflow.description },
|
|
5396
|
+
...security.length > 0 && { security },
|
|
5397
|
+
workflow: ir
|
|
5398
|
+
}
|
|
5399
|
+
};
|
|
5400
|
+
if (options.emitTypeSignatures) {
|
|
5401
|
+
tool.metadata.typescript = emitToolTypeScript(name, description, inputSchema, outputSchema, {
|
|
5402
|
+
maxDepth: Math.max(1, options.maxSchemaDepth ?? 10)
|
|
5403
|
+
});
|
|
5404
|
+
}
|
|
5405
|
+
return tool;
|
|
5406
|
+
}
|
|
5407
|
+
async function fromArazzo(document, options) {
|
|
5408
|
+
const doc = parseArazzoInput(document);
|
|
5409
|
+
validateDocument(doc);
|
|
5410
|
+
const sources = await prepareSources(doc, options);
|
|
5411
|
+
const workflowIds = new Set(doc.workflows.map((w) => w.workflowId));
|
|
5412
|
+
const dependsEdges = /* @__PURE__ */ new Map();
|
|
5413
|
+
const nestedEdges = /* @__PURE__ */ new Map();
|
|
5414
|
+
const declaredSources = new Set(doc.sourceDescriptions.map((s) => s.name));
|
|
5415
|
+
doc.workflows.forEach((workflow, index) => {
|
|
5416
|
+
if (workflow.dependsOn !== void 0 && !Array.isArray(workflow.dependsOn)) {
|
|
5417
|
+
err(`Workflow "${workflow.workflowId}" dependsOn must be an array of workflowIds`, `/workflows/${index}/dependsOn`);
|
|
5418
|
+
}
|
|
5419
|
+
const localTargets = [];
|
|
5420
|
+
for (const target of workflow.dependsOn ?? []) {
|
|
5421
|
+
if (typeof target !== "string") {
|
|
5422
|
+
err(`Workflow "${workflow.workflowId}" dependsOn entries must be strings`, `/workflows/${index}/dependsOn`);
|
|
5423
|
+
}
|
|
5424
|
+
if (target.startsWith("$")) {
|
|
5425
|
+
const ast = parseRuntimeExpression(target, `/workflows/${index}/dependsOn`);
|
|
5426
|
+
if (ast.type !== "sourceDescriptions" || ast.path.length < 2 || !declaredSources.has(ast.path[0])) {
|
|
5427
|
+
err(
|
|
5428
|
+
`Workflow "${workflow.workflowId}" dependsOn "${target}" must reference a declared source ($sourceDescriptions.<name>.<workflowId>)`,
|
|
5429
|
+
`/workflows/${index}/dependsOn`
|
|
5430
|
+
);
|
|
5431
|
+
}
|
|
5432
|
+
continue;
|
|
5433
|
+
}
|
|
5434
|
+
if (!workflowIds.has(target)) {
|
|
5435
|
+
err(`Workflow "${workflow.workflowId}" dependsOn unknown workflow "${target}"`, `/workflows/${index}/dependsOn`);
|
|
5436
|
+
}
|
|
5437
|
+
localTargets.push(target);
|
|
5438
|
+
}
|
|
5439
|
+
dependsEdges.set(workflow.workflowId, localTargets);
|
|
5440
|
+
nestedEdges.set(
|
|
5441
|
+
workflow.workflowId,
|
|
5442
|
+
workflow.steps.filter((s) => s.workflowId !== void 0 && !s.workflowId.startsWith("$")).map((s) => s.workflowId)
|
|
5443
|
+
);
|
|
5444
|
+
});
|
|
5445
|
+
checkCycles(dependsEdges, "dependsOn chain");
|
|
5446
|
+
checkCycles(nestedEdges, "workflow invocation");
|
|
5447
|
+
const ctx = {
|
|
5448
|
+
doc,
|
|
5449
|
+
sources,
|
|
5450
|
+
generateOptions: options.generateOptions ?? {},
|
|
5451
|
+
workflowIds,
|
|
5452
|
+
operationCache: /* @__PURE__ */ new Map()
|
|
5453
|
+
};
|
|
5454
|
+
const tools = [];
|
|
5455
|
+
const usedNames = /* @__PURE__ */ new Set();
|
|
5456
|
+
for (let wIndex = 0; wIndex < doc.workflows.length; wIndex++) {
|
|
5457
|
+
const workflow = doc.workflows[wIndex];
|
|
5458
|
+
const wPath = `/workflows/${wIndex}`;
|
|
5459
|
+
const stepIRs = [];
|
|
5460
|
+
for (let sIndex = 0; sIndex < workflow.steps.length; sIndex++) {
|
|
5461
|
+
stepIRs.push(await buildStepIR(workflow.steps[sIndex], ctx, `${wPath}/steps/${sIndex}`));
|
|
5462
|
+
}
|
|
5463
|
+
let tool = buildWorkflowTool(workflow, stepIRs, ctx, wPath);
|
|
5464
|
+
if (usedNames.has(tool.name)) {
|
|
5465
|
+
const maxLength = ctx.generateOptions.maxToolNameLength ?? 64;
|
|
5466
|
+
let seed = workflow.workflowId;
|
|
5467
|
+
let deduped = normalizeToolName(`${tool.name}_${fnv1aHex(seed)}`, maxLength, seed);
|
|
5468
|
+
while (usedNames.has(deduped)) {
|
|
5469
|
+
seed += "#";
|
|
5470
|
+
deduped = normalizeToolName(`${tool.name}_${fnv1aHex(seed)}`, maxLength, seed);
|
|
5471
|
+
}
|
|
5472
|
+
tool = { ...tool, name: deduped };
|
|
5473
|
+
}
|
|
5474
|
+
usedNames.add(tool.name);
|
|
5475
|
+
tools.push(tool);
|
|
5476
|
+
}
|
|
5477
|
+
return tools;
|
|
5478
|
+
}
|
|
5479
|
+
|
|
3710
5480
|
// src/request-builder.ts
|
|
3711
5481
|
var RESERVED_DECODE = {
|
|
3712
5482
|
"%3A": ":",
|
|
@@ -3943,7 +5713,7 @@ function buildHttpRequest(tool, input, options = {}) {
|
|
|
3943
5713
|
case "body":
|
|
3944
5714
|
hasBody = true;
|
|
3945
5715
|
contentType = contentType ?? mapper.serialization?.contentType ?? "application/json";
|
|
3946
|
-
if (mapper.serialization?.binary) binaryBody = true;
|
|
5716
|
+
if (mapper.serialization?.binary && mapper.wholeBody) binaryBody = true;
|
|
3947
5717
|
if (mapper.wholeBody) {
|
|
3948
5718
|
rawBody = value;
|
|
3949
5719
|
} else {
|
|
@@ -4040,13 +5810,14 @@ function buildHttpRequest(tool, input, options = {}) {
|
|
|
4040
5810
|
// src/sdk.ts
|
|
4041
5811
|
function toSdkTool(tool, wrapper) {
|
|
4042
5812
|
const wrapSchema = wrapper?.fromJsonSchema ?? ((schema) => schema);
|
|
5813
|
+
const outputSchema = tool.outputSchema !== void 0 && tool.outputSchema["type"] === "object" ? tool.outputSchema : void 0;
|
|
4043
5814
|
return [
|
|
4044
5815
|
tool.name,
|
|
4045
5816
|
{
|
|
4046
5817
|
...tool.title !== void 0 && { title: tool.title },
|
|
4047
5818
|
description: tool.description,
|
|
4048
5819
|
inputSchema: wrapSchema(tool.inputSchema),
|
|
4049
|
-
...
|
|
5820
|
+
...outputSchema !== void 0 && { outputSchema: wrapSchema(outputSchema) },
|
|
4050
5821
|
...tool.annotations !== void 0 && { annotations: tool.annotations }
|
|
4051
5822
|
}
|
|
4052
5823
|
];
|
|
@@ -4091,8 +5862,10 @@ function analyzeToolSet(tools, options = {}) {
|
|
|
4091
5862
|
}
|
|
4092
5863
|
// Annotate the CommonJS export names for ESM import in node:
|
|
4093
5864
|
0 && (module.exports = {
|
|
5865
|
+
ArazzoError,
|
|
4094
5866
|
BLOCKED_HOSTNAMES,
|
|
4095
5867
|
BUILTIN_FORMAT_RESOLVERS,
|
|
5868
|
+
CODECALL_RESERVED_NAMESPACES,
|
|
4096
5869
|
GenerationError,
|
|
4097
5870
|
LoadError,
|
|
4098
5871
|
OpenAPIToolError,
|
|
@@ -4119,10 +5892,14 @@ function analyzeToolSet(tools, options = {}) {
|
|
|
4119
5892
|
decodeIpv4MappedIpv6,
|
|
4120
5893
|
defaultLookup,
|
|
4121
5894
|
demoteFormats,
|
|
5895
|
+
deriveSecurityElicitations,
|
|
5896
|
+
dottedNaming,
|
|
5897
|
+
emitToolTypeScript,
|
|
4122
5898
|
enforceClosedObjects,
|
|
4123
5899
|
ensureArrayItems,
|
|
4124
5900
|
estimateToolTokens,
|
|
4125
5901
|
extractExtensionOverrides,
|
|
5902
|
+
fromArazzo,
|
|
4126
5903
|
inferAnnotationsFromMethod,
|
|
4127
5904
|
inlineLocalRefs,
|
|
4128
5905
|
isBlockedAddress,
|
|
@@ -4130,10 +5907,12 @@ function analyzeToolSet(tools, options = {}) {
|
|
|
4130
5907
|
isReferenceObject,
|
|
4131
5908
|
lintDocument,
|
|
4132
5909
|
normalizeSsrfOptions,
|
|
5910
|
+
parseRuntimeExpression,
|
|
4133
5911
|
requireAllProperties,
|
|
4134
5912
|
resolveExtensionEnabled,
|
|
4135
5913
|
resolveSchemaFormats,
|
|
4136
5914
|
safeFetch,
|
|
4137
5915
|
toJsonSchema,
|
|
5916
|
+
toPascalIdentifier,
|
|
4138
5917
|
toSdkTool
|
|
4139
5918
|
});
|