@utcp/http 1.0.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 +96 -0
- package/dist/http_call_template.d.ts +35 -0
- package/dist/http_call_template.js +55 -0
- package/dist/http_communication_protocol.d.ts +88 -0
- package/dist/http_communication_protocol.js +337 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +41 -0
- package/dist/openapi_converter.d.ts +117 -0
- package/dist/openapi_converter.js +535 -0
- package/dist/sse_call_template.d.ts +53 -0
- package/dist/sse_call_template.js +55 -0
- package/dist/sse_communication_protocol.d.ts +47 -0
- package/dist/sse_communication_protocol.js +157 -0
- package/dist/streamable_http_call_template.d.ts +55 -0
- package/dist/streamable_http_call_template.js +57 -0
- package/dist/streamable_http_communication_protocol.d.ts +47 -0
- package/dist/streamable_http_communication_protocol.js +154 -0
- package/package.json +53 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { UtcpManual } from '@utcp/core/data/utcp_manual';
|
|
2
|
+
import { Auth } from '@utcp/core/data/auth';
|
|
3
|
+
interface OpenApiConverterOptions {
|
|
4
|
+
specUrl?: string;
|
|
5
|
+
callTemplateName?: string;
|
|
6
|
+
authTools?: Auth;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* REQUIRED
|
|
10
|
+
* Converts OpenAPI specifications into UTCP tool definitions.
|
|
11
|
+
*
|
|
12
|
+
* Processes OpenAPI 2.0 and 3.0 specifications to generate equivalent UTCP
|
|
13
|
+
* tools, handling schema resolution, authentication mapping, and proper
|
|
14
|
+
* HTTP call_template configuration. Each operation in the OpenAPI spec becomes
|
|
15
|
+
* a UTCP tool with appropriate input/output schemas.
|
|
16
|
+
*/
|
|
17
|
+
export declare class OpenApiConverter {
|
|
18
|
+
private spec;
|
|
19
|
+
private spec_url;
|
|
20
|
+
private auth_tools;
|
|
21
|
+
private placeholder_counter;
|
|
22
|
+
private call_template_name;
|
|
23
|
+
/**
|
|
24
|
+
* Initializes the OpenAPI converter.
|
|
25
|
+
*
|
|
26
|
+
* @param openapi_spec Parsed OpenAPI specification as a dictionary.
|
|
27
|
+
* @param options Optional settings including spec_url, call_template_name, and auth_tools.
|
|
28
|
+
* - specUrl: URL where the specification was retrieved from.
|
|
29
|
+
* - callTemplateName: Custom name for the call_template if spec title not provided.
|
|
30
|
+
* - authTools: Optional auth configuration for generated tools.
|
|
31
|
+
*/
|
|
32
|
+
constructor(openapi_spec: Record<string, any>, options?: OpenApiConverterOptions);
|
|
33
|
+
private _generateUuid;
|
|
34
|
+
private _incrementPlaceholderCounter;
|
|
35
|
+
private _getPlaceholder;
|
|
36
|
+
/**
|
|
37
|
+
* Parses the OpenAPI specification and returns a UtcpManual.
|
|
38
|
+
* @returns A UTCP manual containing tools derived from the OpenAPI specification.
|
|
39
|
+
*/
|
|
40
|
+
/**
|
|
41
|
+
* REQUIRED
|
|
42
|
+
* Converts the loaded OpenAPI specification into a UtcpManual.
|
|
43
|
+
*/
|
|
44
|
+
convert(): UtcpManual;
|
|
45
|
+
/**
|
|
46
|
+
* Resolves a local JSON reference within the OpenAPI spec.
|
|
47
|
+
* @param ref The reference string (e.g., '#/components/schemas/Pet').
|
|
48
|
+
* @returns The resolved schema object.
|
|
49
|
+
*/
|
|
50
|
+
private _resolveRef;
|
|
51
|
+
/**
|
|
52
|
+
* Recursively resolves all $refs in a schema object, preventing infinite loops.
|
|
53
|
+
* @param schema The schema object that may contain references.
|
|
54
|
+
* @param visitedRefs A set of references already visited to detect cycles.
|
|
55
|
+
* @returns The resolved schema with all references replaced by their actual values.
|
|
56
|
+
*/
|
|
57
|
+
private _resolveSchema;
|
|
58
|
+
/**
|
|
59
|
+
* Creates a Tool object from an OpenAPI operation.
|
|
60
|
+
* @param path The API path.
|
|
61
|
+
* @param method The HTTP method (GET, POST, etc.).
|
|
62
|
+
* @param operation The operation definition from OpenAPI.
|
|
63
|
+
* @param baseUrl The base URL for the API.
|
|
64
|
+
* @returns A Tool object or null if operationId is not defined.
|
|
65
|
+
*/
|
|
66
|
+
private _createTool;
|
|
67
|
+
/**
|
|
68
|
+
* Extracts the input schema, header fields, and body field from an OpenAPI operation.
|
|
69
|
+
* - Merges path-level and operation-level parameters.
|
|
70
|
+
* - Resolves $ref for parameters.
|
|
71
|
+
* - Supports OpenAPI 2.0 body parameters and 3.0 requestBody.
|
|
72
|
+
* @param path The API path.
|
|
73
|
+
* @param operation The OpenAPI operation object.
|
|
74
|
+
* @returns An object containing the inputs schema, a list of header field names, and the body field name (if any).
|
|
75
|
+
*/
|
|
76
|
+
private _extractInputs;
|
|
77
|
+
/**
|
|
78
|
+
* Extracts the output schema from an OpenAPI operation, resolving refs.
|
|
79
|
+
* @param operation The OpenAPI operation object.
|
|
80
|
+
* @returns The output schema.
|
|
81
|
+
*/
|
|
82
|
+
private _extractOutputs;
|
|
83
|
+
/**
|
|
84
|
+
* Extracts authentication information from OpenAPI operation and global security schemes.
|
|
85
|
+
* Uses auth_tools configuration when compatible with OpenAPI auth requirements.
|
|
86
|
+
* Supports both OpenAPI 2.0 and 3.0 security schemes.
|
|
87
|
+
* @param operation The OpenAPI operation object.
|
|
88
|
+
* @returns An Auth object or undefined if no authentication is specified.
|
|
89
|
+
*/
|
|
90
|
+
private _extractAuth;
|
|
91
|
+
/**
|
|
92
|
+
* Checks if auth_tools configuration is compatible with OpenAPI auth requirements.
|
|
93
|
+
*
|
|
94
|
+
* @param openapiAuth Auth generated from OpenAPI security scheme
|
|
95
|
+
* @param authTools Auth configuration from manual call template
|
|
96
|
+
* @returns True if compatible and auth_tools should be used, False otherwise
|
|
97
|
+
*/
|
|
98
|
+
private _isAuthCompatible;
|
|
99
|
+
/**
|
|
100
|
+
* Gets security schemes supporting both OpenAPI 2.0 and 3.0.
|
|
101
|
+
* @returns A record of security schemes.
|
|
102
|
+
*/
|
|
103
|
+
private _getSecuritySchemes;
|
|
104
|
+
/**
|
|
105
|
+
* Creates an Auth object from an OpenAPI security scheme.
|
|
106
|
+
* @param scheme The security scheme object.
|
|
107
|
+
* @param schemeName The name of the security scheme.
|
|
108
|
+
* @returns An Auth object or undefined if the scheme is not supported.
|
|
109
|
+
*/
|
|
110
|
+
private _createAuthFromScheme;
|
|
111
|
+
}
|
|
112
|
+
declare global {
|
|
113
|
+
interface String {
|
|
114
|
+
lstrip(chars: string): string;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
export {};
|
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAPI specification converter for UTCP tool generation.
|
|
3
|
+
*
|
|
4
|
+
* This module provides functionality to convert OpenAPI specifications (both 2.0
|
|
5
|
+
* and 3.0) into UTCP tool definitions. It handles schema resolution, authentication
|
|
6
|
+
* mapping, and proper tool creation from REST API specifications.
|
|
7
|
+
*
|
|
8
|
+
* Key Features:
|
|
9
|
+
* - OpenAPI 2.0 and 3.0 specification support.
|
|
10
|
+
* - Automatic JSON reference ($ref) resolution.
|
|
11
|
+
* - Authentication scheme mapping (API key, Basic, OAuth2).
|
|
12
|
+
* - Input/output schema extraction from OpenAPI schemas.
|
|
13
|
+
* - URL path parameter handling.
|
|
14
|
+
* - Request body and header field mapping.
|
|
15
|
+
* - Call template name generation from specification metadata.
|
|
16
|
+
*
|
|
17
|
+
* The converter creates UTCP tools that can be used to interact with REST APIs
|
|
18
|
+
* defined by OpenAPI specifications, providing a bridge between OpenAPI and UTCP.
|
|
19
|
+
*/
|
|
20
|
+
// packages/http/src/openapi_converter.ts
|
|
21
|
+
import { JsonSchemaSchema } from '@utcp/core/data/tool';
|
|
22
|
+
import { UtcpManualSerializer } from '@utcp/core/data/utcp_manual';
|
|
23
|
+
/**
|
|
24
|
+
* REQUIRED
|
|
25
|
+
* Converts OpenAPI specifications into UTCP tool definitions.
|
|
26
|
+
*
|
|
27
|
+
* Processes OpenAPI 2.0 and 3.0 specifications to generate equivalent UTCP
|
|
28
|
+
* tools, handling schema resolution, authentication mapping, and proper
|
|
29
|
+
* HTTP call_template configuration. Each operation in the OpenAPI spec becomes
|
|
30
|
+
* a UTCP tool with appropriate input/output schemas.
|
|
31
|
+
*/
|
|
32
|
+
export class OpenApiConverter {
|
|
33
|
+
spec;
|
|
34
|
+
spec_url;
|
|
35
|
+
auth_tools;
|
|
36
|
+
placeholder_counter = 0;
|
|
37
|
+
call_template_name;
|
|
38
|
+
/**
|
|
39
|
+
* Initializes the OpenAPI converter.
|
|
40
|
+
*
|
|
41
|
+
* @param openapi_spec Parsed OpenAPI specification as a dictionary.
|
|
42
|
+
* @param options Optional settings including spec_url, call_template_name, and auth_tools.
|
|
43
|
+
* - specUrl: URL where the specification was retrieved from.
|
|
44
|
+
* - callTemplateName: Custom name for the call_template if spec title not provided.
|
|
45
|
+
* - authTools: Optional auth configuration for generated tools.
|
|
46
|
+
*/
|
|
47
|
+
constructor(openapi_spec, options) {
|
|
48
|
+
this.spec = openapi_spec;
|
|
49
|
+
this.spec_url = options?.specUrl;
|
|
50
|
+
this.auth_tools = options?.authTools;
|
|
51
|
+
this.placeholder_counter = 0;
|
|
52
|
+
let callTemplateName = options?.callTemplateName;
|
|
53
|
+
if (!callTemplateName) {
|
|
54
|
+
callTemplateName = 'openapi_call_template_' + this._generateUuid();
|
|
55
|
+
}
|
|
56
|
+
const title = openapi_spec.info?.title || callTemplateName;
|
|
57
|
+
// Replace characters that are invalid for identifiers
|
|
58
|
+
const invalidChars = " -.,!?'\"\\/() []{}#@$%^&*+=~`|;:<>";
|
|
59
|
+
this.call_template_name = Array.from(String(title)).map(c => invalidChars.includes(c) ? '_' : c).join('');
|
|
60
|
+
}
|
|
61
|
+
_generateUuid() {
|
|
62
|
+
// Simple UUID hex generator without dashes
|
|
63
|
+
return 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'.replace(/x/g, () => {
|
|
64
|
+
return Math.floor(Math.random() * 16).toString(16);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
_incrementPlaceholderCounter() {
|
|
68
|
+
this.placeholder_counter++;
|
|
69
|
+
return this.placeholder_counter;
|
|
70
|
+
}
|
|
71
|
+
_getPlaceholder(baseName) {
|
|
72
|
+
return `\$\{${baseName.toUpperCase()}_${this.placeholder_counter}\}`;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Parses the OpenAPI specification and returns a UtcpManual.
|
|
76
|
+
* @returns A UTCP manual containing tools derived from the OpenAPI specification.
|
|
77
|
+
*/
|
|
78
|
+
/**
|
|
79
|
+
* REQUIRED
|
|
80
|
+
* Converts the loaded OpenAPI specification into a UtcpManual.
|
|
81
|
+
*/
|
|
82
|
+
convert() {
|
|
83
|
+
this.placeholder_counter = 0;
|
|
84
|
+
const tools = [];
|
|
85
|
+
let baseUrl = '/';
|
|
86
|
+
const servers = this.spec.servers;
|
|
87
|
+
if (servers && Array.isArray(servers) && servers.length > 0 && servers[0].url) {
|
|
88
|
+
baseUrl = servers[0].url;
|
|
89
|
+
}
|
|
90
|
+
else if (this.spec_url) {
|
|
91
|
+
try {
|
|
92
|
+
const parsedUrl = new URL(this.spec_url);
|
|
93
|
+
baseUrl = `${parsedUrl.protocol}//${parsedUrl.host}`;
|
|
94
|
+
}
|
|
95
|
+
catch (e) {
|
|
96
|
+
console.error(`[OpenApiConverter] Invalid spec_url provided: ${this.spec_url}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
console.warn("[OpenApiConverter] No server info or spec URL provided. Using fallback base URL: '/'");
|
|
101
|
+
}
|
|
102
|
+
const paths = this.spec.paths || {};
|
|
103
|
+
for (const [path, pathItem] of Object.entries(paths)) {
|
|
104
|
+
for (const [method, operation] of Object.entries(pathItem)) {
|
|
105
|
+
if (['get', 'post', 'put', 'delete', 'patch'].includes(method.toLowerCase())) {
|
|
106
|
+
const tool = this._createTool(path, method, operation, baseUrl);
|
|
107
|
+
if (tool) {
|
|
108
|
+
tools.push(tool);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// Use the serializer to leverage default values for utcp_version and manual_version
|
|
114
|
+
return new UtcpManualSerializer().validateDict({ tools });
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Resolves a local JSON reference within the OpenAPI spec.
|
|
118
|
+
* @param ref The reference string (e.g., '#/components/schemas/Pet').
|
|
119
|
+
* @returns The resolved schema object.
|
|
120
|
+
*/
|
|
121
|
+
_resolveRef(ref) {
|
|
122
|
+
if (!ref.startsWith('#/')) {
|
|
123
|
+
// External or non-local references are not fully supported by this simple resolver.
|
|
124
|
+
// For a robust implementation, external fetches would be needed.
|
|
125
|
+
return { $ref: ref }; // Return the ref itself to indicate it's unresolved
|
|
126
|
+
}
|
|
127
|
+
const parts = ref.substring(2).split('/');
|
|
128
|
+
let node = this.spec;
|
|
129
|
+
for (const part of parts) {
|
|
130
|
+
// Decode URI components in case the part contains slashes or other special characters
|
|
131
|
+
const decodedPart = decodeURIComponent(part);
|
|
132
|
+
if (node[decodedPart] === undefined) {
|
|
133
|
+
// Reference not found, return the ref to prevent crashing
|
|
134
|
+
return { $ref: ref };
|
|
135
|
+
}
|
|
136
|
+
node = node[decodedPart];
|
|
137
|
+
}
|
|
138
|
+
return node;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Recursively resolves all $refs in a schema object, preventing infinite loops.
|
|
142
|
+
* @param schema The schema object that may contain references.
|
|
143
|
+
* @param visitedRefs A set of references already visited to detect cycles.
|
|
144
|
+
* @returns The resolved schema with all references replaced by their actual values.
|
|
145
|
+
*/
|
|
146
|
+
_resolveSchema(schema, visitedRefs = new Set()) {
|
|
147
|
+
if (schema === null || typeof schema !== 'object') {
|
|
148
|
+
return schema;
|
|
149
|
+
}
|
|
150
|
+
if (Array.isArray(schema)) {
|
|
151
|
+
return schema.map(item => this._resolveSchema(item, visitedRefs));
|
|
152
|
+
}
|
|
153
|
+
if ('$ref' in schema && typeof schema.$ref === 'string') {
|
|
154
|
+
const ref = schema.$ref;
|
|
155
|
+
if (visitedRefs.has(ref)) {
|
|
156
|
+
// Cycle detected, return the reference itself to break the loop
|
|
157
|
+
return { $ref: ref };
|
|
158
|
+
}
|
|
159
|
+
visitedRefs.add(ref);
|
|
160
|
+
const resolvedRef = this._resolveRef(ref);
|
|
161
|
+
// Recursively resolve the content of the resolved reference
|
|
162
|
+
return this._resolveSchema(resolvedRef, visitedRefs);
|
|
163
|
+
}
|
|
164
|
+
const newSchema = {};
|
|
165
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
166
|
+
newSchema[key] = this._resolveSchema(value, visitedRefs);
|
|
167
|
+
}
|
|
168
|
+
return newSchema;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Creates a Tool object from an OpenAPI operation.
|
|
172
|
+
* @param path The API path.
|
|
173
|
+
* @param method The HTTP method (GET, POST, etc.).
|
|
174
|
+
* @param operation The operation definition from OpenAPI.
|
|
175
|
+
* @param baseUrl The base URL for the API.
|
|
176
|
+
* @returns A Tool object or null if operationId is not defined.
|
|
177
|
+
*/
|
|
178
|
+
_createTool(path, method, operation, baseUrl) {
|
|
179
|
+
const operationId = operation.operationId;
|
|
180
|
+
if (!operationId) {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
const description = operation.summary || operation.description || '';
|
|
184
|
+
const tags = operation.tags || [];
|
|
185
|
+
const { inputs, headerFields, bodyField } = this._extractInputs(path, operation);
|
|
186
|
+
const outputs = this._extractOutputs(operation);
|
|
187
|
+
const auth = this._extractAuth(operation);
|
|
188
|
+
const fullUrl = `${baseUrl.replace(/\/$/, '')}/${path.lstrip('/')}`;
|
|
189
|
+
const callTemplate = {
|
|
190
|
+
name: this.call_template_name,
|
|
191
|
+
call_template_type: 'http',
|
|
192
|
+
http_method: method.toUpperCase(),
|
|
193
|
+
url: fullUrl,
|
|
194
|
+
body_field: bodyField ?? undefined,
|
|
195
|
+
header_fields: headerFields.length > 0 ? headerFields : undefined,
|
|
196
|
+
auth,
|
|
197
|
+
content_type: 'application/json',
|
|
198
|
+
timeout: 30
|
|
199
|
+
};
|
|
200
|
+
return {
|
|
201
|
+
name: operationId,
|
|
202
|
+
description,
|
|
203
|
+
inputs: JsonSchemaSchema.parse(inputs),
|
|
204
|
+
outputs: JsonSchemaSchema.parse(outputs),
|
|
205
|
+
tags,
|
|
206
|
+
tool_call_template: callTemplate
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Extracts the input schema, header fields, and body field from an OpenAPI operation.
|
|
211
|
+
* - Merges path-level and operation-level parameters.
|
|
212
|
+
* - Resolves $ref for parameters.
|
|
213
|
+
* - Supports OpenAPI 2.0 body parameters and 3.0 requestBody.
|
|
214
|
+
* @param path The API path.
|
|
215
|
+
* @param operation The OpenAPI operation object.
|
|
216
|
+
* @returns An object containing the inputs schema, a list of header field names, and the body field name (if any).
|
|
217
|
+
*/
|
|
218
|
+
_extractInputs(path, operation) {
|
|
219
|
+
const properties = {};
|
|
220
|
+
const required = [];
|
|
221
|
+
const headerFields = [];
|
|
222
|
+
let bodyField = null;
|
|
223
|
+
// Merge path-level and operation-level parameters
|
|
224
|
+
const pathItem = this.spec.paths?.[path] || {};
|
|
225
|
+
const allParams = [...(pathItem.parameters || []), ...(operation.parameters || [])];
|
|
226
|
+
for (const param of allParams) {
|
|
227
|
+
const resolvedParam = this._resolveSchema(param);
|
|
228
|
+
const paramName = resolvedParam.name;
|
|
229
|
+
if (!paramName)
|
|
230
|
+
continue;
|
|
231
|
+
if (resolvedParam.in === 'header') {
|
|
232
|
+
headerFields.push(paramName);
|
|
233
|
+
}
|
|
234
|
+
// OpenAPI 2.0 body parameter (deprecated in 3.0, but still possible)
|
|
235
|
+
if (resolvedParam.in === 'body') {
|
|
236
|
+
bodyField = 'body';
|
|
237
|
+
const jsonSchema = this._resolveSchema(resolvedParam.schema || {});
|
|
238
|
+
properties[bodyField] = {
|
|
239
|
+
description: resolvedParam.description || 'Request body',
|
|
240
|
+
...jsonSchema,
|
|
241
|
+
};
|
|
242
|
+
if (resolvedParam.required) {
|
|
243
|
+
required.push(bodyField);
|
|
244
|
+
}
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
// Other parameters (query, path, header, cookie)
|
|
248
|
+
const schema = this._resolveSchema(resolvedParam.schema || {});
|
|
249
|
+
// For OpenAPI 2.0, non-body parameters might have type/items directly on the parameter object
|
|
250
|
+
if (!schema.type && resolvedParam.type)
|
|
251
|
+
schema.type = resolvedParam.type;
|
|
252
|
+
if (!schema.items && resolvedParam.items)
|
|
253
|
+
schema.items = resolvedParam.items;
|
|
254
|
+
if (!schema.enum && resolvedParam.enum)
|
|
255
|
+
schema.enum = resolvedParam.enum;
|
|
256
|
+
properties[paramName] = {
|
|
257
|
+
description: resolvedParam.description || '',
|
|
258
|
+
...schema,
|
|
259
|
+
};
|
|
260
|
+
if (resolvedParam.required) {
|
|
261
|
+
required.push(paramName);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
// Handle request body (OpenAPI 3.0 equivalent of 'body' parameter)
|
|
265
|
+
const requestBody = operation.requestBody;
|
|
266
|
+
if (requestBody) {
|
|
267
|
+
const resolvedBody = this._resolveSchema(requestBody);
|
|
268
|
+
const content = resolvedBody.content || {};
|
|
269
|
+
const jsonSchema = content['application/json']?.schema || content['application/x-www-form-urlencoded']?.schema;
|
|
270
|
+
if (jsonSchema) {
|
|
271
|
+
bodyField = 'body';
|
|
272
|
+
properties[bodyField] = {
|
|
273
|
+
description: resolvedBody.description || 'Request body',
|
|
274
|
+
...this._resolveSchema(jsonSchema),
|
|
275
|
+
};
|
|
276
|
+
if (resolvedBody.required) {
|
|
277
|
+
required.push(bodyField);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
const inputs = {
|
|
282
|
+
type: 'object',
|
|
283
|
+
properties,
|
|
284
|
+
required: required.length > 0 ? required : undefined
|
|
285
|
+
};
|
|
286
|
+
return { inputs, headerFields, bodyField };
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Extracts the output schema from an OpenAPI operation, resolving refs.
|
|
290
|
+
* @param operation The OpenAPI operation object.
|
|
291
|
+
* @returns The output schema.
|
|
292
|
+
*/
|
|
293
|
+
_extractOutputs(operation) {
|
|
294
|
+
const responses = operation.responses || {};
|
|
295
|
+
// Prioritize 200/201 responses, then fall back to 'default'
|
|
296
|
+
const successResponse = responses['200'] || responses['201'] || responses['default'];
|
|
297
|
+
if (!successResponse) {
|
|
298
|
+
return {};
|
|
299
|
+
}
|
|
300
|
+
const resolvedResponse = this._resolveSchema(successResponse);
|
|
301
|
+
let jsonSchema = null;
|
|
302
|
+
if ('content' in resolvedResponse) { // OpenAPI 3.0
|
|
303
|
+
const content = resolvedResponse.content || {};
|
|
304
|
+
jsonSchema = content['application/json']?.schema || content['text/plain']?.schema;
|
|
305
|
+
if (!jsonSchema && Object.keys(content).length > 0) {
|
|
306
|
+
// Fallback to first content type's schema, with a type guard
|
|
307
|
+
const firstContentTypeValue = Object.values(content)[0];
|
|
308
|
+
if (typeof firstContentTypeValue === 'object' && firstContentTypeValue !== null && 'schema' in firstContentTypeValue) {
|
|
309
|
+
jsonSchema = firstContentTypeValue.schema;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
else if ('schema' in resolvedResponse) { // OpenAPI 2.0
|
|
314
|
+
jsonSchema = resolvedResponse.schema;
|
|
315
|
+
}
|
|
316
|
+
if (!jsonSchema) {
|
|
317
|
+
return {};
|
|
318
|
+
}
|
|
319
|
+
const resolvedJsonSchema = this._resolveSchema(jsonSchema);
|
|
320
|
+
const schemaArgs = {
|
|
321
|
+
type: resolvedJsonSchema.type || 'object',
|
|
322
|
+
properties: resolvedJsonSchema.properties || undefined,
|
|
323
|
+
required: resolvedJsonSchema.required || undefined,
|
|
324
|
+
description: resolvedJsonSchema.description || undefined,
|
|
325
|
+
title: resolvedJsonSchema.title || undefined,
|
|
326
|
+
items: resolvedJsonSchema.items || undefined,
|
|
327
|
+
enum: resolvedJsonSchema.enum || undefined,
|
|
328
|
+
minimum: resolvedJsonSchema.minimum || undefined,
|
|
329
|
+
maximum: resolvedJsonSchema.maximum || undefined,
|
|
330
|
+
format: resolvedJsonSchema.format || undefined,
|
|
331
|
+
};
|
|
332
|
+
return schemaArgs;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Extracts authentication information from OpenAPI operation and global security schemes.
|
|
336
|
+
* Uses auth_tools configuration when compatible with OpenAPI auth requirements.
|
|
337
|
+
* Supports both OpenAPI 2.0 and 3.0 security schemes.
|
|
338
|
+
* @param operation The OpenAPI operation object.
|
|
339
|
+
* @returns An Auth object or undefined if no authentication is specified.
|
|
340
|
+
*/
|
|
341
|
+
_extractAuth(operation) {
|
|
342
|
+
// First check for operation-level security requirements
|
|
343
|
+
let securityRequirements = operation.security || [];
|
|
344
|
+
// If no operation-level security, check global security requirements
|
|
345
|
+
if (!securityRequirements.length) {
|
|
346
|
+
securityRequirements = this.spec.security || [];
|
|
347
|
+
}
|
|
348
|
+
// If no security requirements, return undefined (endpoint is public)
|
|
349
|
+
if (!securityRequirements.length) {
|
|
350
|
+
return undefined;
|
|
351
|
+
}
|
|
352
|
+
// Generate auth from OpenAPI security schemes - support both OpenAPI 2.0 and 3.0
|
|
353
|
+
const securitySchemes = this._getSecuritySchemes();
|
|
354
|
+
// Process the first security requirement (most common case)
|
|
355
|
+
// Each security requirement is a dict with scheme name as key
|
|
356
|
+
for (const securityReq of securityRequirements) {
|
|
357
|
+
for (const schemeName of Object.keys(securityReq)) {
|
|
358
|
+
if (schemeName in securitySchemes) {
|
|
359
|
+
const scheme = securitySchemes[schemeName];
|
|
360
|
+
const openapiAuth = this._createAuthFromScheme(scheme, schemeName);
|
|
361
|
+
// If compatible with auth_tools, use actual values from manual call template
|
|
362
|
+
if (this._isAuthCompatible(openapiAuth, this.auth_tools)) {
|
|
363
|
+
return this.auth_tools;
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
return openapiAuth; // Use placeholder from OpenAPI scheme
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return undefined;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Checks if auth_tools configuration is compatible with OpenAPI auth requirements.
|
|
375
|
+
*
|
|
376
|
+
* @param openapiAuth Auth generated from OpenAPI security scheme
|
|
377
|
+
* @param authTools Auth configuration from manual call template
|
|
378
|
+
* @returns True if compatible and auth_tools should be used, False otherwise
|
|
379
|
+
*/
|
|
380
|
+
_isAuthCompatible(openapiAuth, authTools) {
|
|
381
|
+
if (!openapiAuth || !authTools) {
|
|
382
|
+
return false;
|
|
383
|
+
}
|
|
384
|
+
// Must be same auth type
|
|
385
|
+
if (openapiAuth.auth_type !== authTools.auth_type) {
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
// For API Key auth, check header name and location compatibility
|
|
389
|
+
if ('var_name' in openapiAuth && 'var_name' in authTools) {
|
|
390
|
+
const openapiVar = openapiAuth.var_name?.toLowerCase() || '';
|
|
391
|
+
const toolsVar = authTools.var_name?.toLowerCase() || '';
|
|
392
|
+
if (openapiVar !== toolsVar) {
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
395
|
+
if ('location' in openapiAuth && 'location' in authTools) {
|
|
396
|
+
if (openapiAuth.location !== authTools.location) {
|
|
397
|
+
return false;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return true;
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Gets security schemes supporting both OpenAPI 2.0 and 3.0.
|
|
405
|
+
* @returns A record of security schemes.
|
|
406
|
+
*/
|
|
407
|
+
_getSecuritySchemes() {
|
|
408
|
+
// OpenAPI 3.0 format
|
|
409
|
+
if ('components' in this.spec) {
|
|
410
|
+
return this.spec.components?.securitySchemes || {};
|
|
411
|
+
}
|
|
412
|
+
// OpenAPI 2.0 format
|
|
413
|
+
return this.spec.securityDefinitions || {};
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Creates an Auth object from an OpenAPI security scheme.
|
|
417
|
+
* @param scheme The security scheme object.
|
|
418
|
+
* @param schemeName The name of the security scheme.
|
|
419
|
+
* @returns An Auth object or undefined if the scheme is not supported.
|
|
420
|
+
*/
|
|
421
|
+
_createAuthFromScheme(scheme, schemeName) {
|
|
422
|
+
const schemeType = (scheme.type || '').toLowerCase();
|
|
423
|
+
if (schemeType === 'apikey') {
|
|
424
|
+
// For API key auth, use the parameter name from the OpenAPI spec
|
|
425
|
+
const location = (scheme.in || 'header');
|
|
426
|
+
const paramName = scheme.name || 'Authorization';
|
|
427
|
+
// Use the current counter value for the placeholder
|
|
428
|
+
const apiKeyPlaceholder = this._getPlaceholder('API_KEY');
|
|
429
|
+
// Increment the counter after using it
|
|
430
|
+
this._incrementPlaceholderCounter();
|
|
431
|
+
return {
|
|
432
|
+
auth_type: 'api_key',
|
|
433
|
+
api_key: apiKeyPlaceholder,
|
|
434
|
+
var_name: paramName,
|
|
435
|
+
location,
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
if (schemeType === 'basic') {
|
|
439
|
+
// OpenAPI 2.0 format: type: basic
|
|
440
|
+
// Use the current counter value for both placeholders
|
|
441
|
+
const usernamePlaceholder = this._getPlaceholder('USERNAME');
|
|
442
|
+
const passwordPlaceholder = this._getPlaceholder('PASSWORD');
|
|
443
|
+
// Increment the counter after using it
|
|
444
|
+
this._incrementPlaceholderCounter();
|
|
445
|
+
return {
|
|
446
|
+
auth_type: 'basic',
|
|
447
|
+
username: usernamePlaceholder,
|
|
448
|
+
password: passwordPlaceholder,
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
if (schemeType === 'http') {
|
|
452
|
+
// OpenAPI 3.0 format: type: http with scheme
|
|
453
|
+
const httpScheme = (scheme.scheme || '').toLowerCase();
|
|
454
|
+
if (httpScheme === 'basic') {
|
|
455
|
+
// For basic auth, use conventional environment variable names
|
|
456
|
+
// Use the current counter value for both placeholders
|
|
457
|
+
const usernamePlaceholder = this._getPlaceholder('USERNAME');
|
|
458
|
+
const passwordPlaceholder = this._getPlaceholder('PASSWORD');
|
|
459
|
+
// Increment the counter after using it
|
|
460
|
+
this._incrementPlaceholderCounter();
|
|
461
|
+
return {
|
|
462
|
+
auth_type: 'basic',
|
|
463
|
+
username: usernamePlaceholder,
|
|
464
|
+
password: passwordPlaceholder,
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
else if (httpScheme === 'bearer') {
|
|
468
|
+
// Treat bearer tokens as API keys
|
|
469
|
+
// Use the current counter value for the placeholder
|
|
470
|
+
const apiKeyPlaceholder = this._getPlaceholder('API_KEY');
|
|
471
|
+
// Increment the counter after using it
|
|
472
|
+
this._incrementPlaceholderCounter();
|
|
473
|
+
return {
|
|
474
|
+
auth_type: 'api_key',
|
|
475
|
+
api_key: `Bearer ${apiKeyPlaceholder}`,
|
|
476
|
+
var_name: 'Authorization',
|
|
477
|
+
location: 'header',
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
if (schemeType === 'oauth2') {
|
|
482
|
+
// Handle both OpenAPI 2.0 and 3.0 OAuth2 formats
|
|
483
|
+
const flows = scheme.flows || {};
|
|
484
|
+
// OpenAPI 3.0 format
|
|
485
|
+
if (Object.keys(flows).length > 0) {
|
|
486
|
+
for (const [flowType, flowConfig] of Object.entries(flows)) {
|
|
487
|
+
// Support both old and new flow names
|
|
488
|
+
if (['authorizationCode', 'accessCode', 'clientCredentials', 'application'].includes(flowType)) {
|
|
489
|
+
const tokenUrl = flowConfig.tokenUrl;
|
|
490
|
+
if (tokenUrl) {
|
|
491
|
+
// Use the current counter value for both placeholders
|
|
492
|
+
const clientIdPlaceholder = this._getPlaceholder('CLIENT_ID');
|
|
493
|
+
const clientSecretPlaceholder = this._getPlaceholder('CLIENT_SECRET');
|
|
494
|
+
// Increment the counter after using it
|
|
495
|
+
this._incrementPlaceholderCounter();
|
|
496
|
+
const scopes = flowConfig.scopes || {};
|
|
497
|
+
return {
|
|
498
|
+
auth_type: 'oauth2',
|
|
499
|
+
token_url: tokenUrl,
|
|
500
|
+
client_id: clientIdPlaceholder,
|
|
501
|
+
client_secret: clientSecretPlaceholder,
|
|
502
|
+
scope: Object.keys(scopes).length > 0 ? Object.keys(scopes).join(' ') : undefined,
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
// OpenAPI 2.0 format fallback
|
|
509
|
+
const tokenUrl = scheme.tokenUrl;
|
|
510
|
+
if (tokenUrl) {
|
|
511
|
+
const clientIdPlaceholder = this._getPlaceholder('CLIENT_ID');
|
|
512
|
+
const clientSecretPlaceholder = this._getPlaceholder('CLIENT_SECRET');
|
|
513
|
+
this._incrementPlaceholderCounter();
|
|
514
|
+
const scopes = scheme.scopes || {};
|
|
515
|
+
return {
|
|
516
|
+
auth_type: 'oauth2',
|
|
517
|
+
token_url: tokenUrl,
|
|
518
|
+
client_id: clientIdPlaceholder,
|
|
519
|
+
client_secret: clientSecretPlaceholder,
|
|
520
|
+
scope: Object.keys(scopes).length > 0 ? Object.keys(scopes).join(' ') : undefined,
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return undefined;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
if (!String.prototype.lstrip) {
|
|
528
|
+
String.prototype.lstrip = function (chars) {
|
|
529
|
+
let result = this;
|
|
530
|
+
while (result.startsWith(chars)) {
|
|
531
|
+
result = result.substring(chars.length);
|
|
532
|
+
}
|
|
533
|
+
return result;
|
|
534
|
+
};
|
|
535
|
+
}
|