-
-
Notifications
You must be signed in to change notification settings - Fork 24.5k
Expand file tree
/
Copy pathOpenAPIToolkit.ts
More file actions
566 lines (529 loc) · 22.6 KB
/
OpenAPIToolkit.ts
File metadata and controls
566 lines (529 loc) · 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import { load } from 'js-yaml'
import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface'
import { getFileFromStorage, getVars, IDatabaseEntity, IVariable } from '../../../src'
import $RefParser from '@apidevtools/json-schema-ref-parser'
import { z, ZodSchema, ZodTypeAny } from 'zod'
import { defaultCode, DynamicStructuredTool, howToUseCode } from './core'
import { DataSource } from 'typeorm'
import { secureFetch } from '../../../src/httpSecurity'
class OpenAPIToolkit_Tools implements INode {
label: string
name: string
version: number
description: string
type: string
icon: string
category: string
baseClasses: string[]
credential: INodeParams
inputs: INodeParams[]
constructor() {
this.label = 'OpenAPI Toolkit'
this.name = 'openAPIToolkit'
this.version = 2.1
this.type = 'OpenAPIToolkit'
this.icon = 'openapi.svg'
this.category = 'Tools'
this.description = 'Load OpenAPI specification, and converts each API endpoint to a tool'
this.inputs = [
{
label: 'Input Type',
name: 'inputType',
type: 'options',
options: [
{
label: 'Upload File',
name: 'file'
},
{
label: 'Provide Link',
name: 'link'
}
],
default: 'file',
description: 'Choose how to provide the OpenAPI specification'
},
{
label: 'OpenAPI File',
name: 'openApiFile',
type: 'file',
fileType: '.yaml,.json',
description: 'Upload your OpenAPI specification file (YAML or JSON)',
show: {
inputType: 'file'
}
},
{
label: 'OpenAPI Link',
name: 'openApiLink',
type: 'string',
placeholder: 'https://api.example.com/openapi.yaml or https://api.example.com/openapi.json',
description: 'Provide a link to your OpenAPI specification (YAML or JSON)',
show: {
inputType: 'link'
}
},
{
label: 'Server',
name: 'selectedServer',
type: 'asyncOptions',
loadMethod: 'listServers',
description: 'Select which server to use for API calls',
refresh: true
},
{
label: 'Available Endpoints',
name: 'selectedEndpoints',
type: 'asyncMultiOptions',
loadMethod: 'listEndpoints',
description: 'Select which endpoints to expose as tools',
refresh: true
},
{
label: 'Return Direct',
name: 'returnDirect',
description: 'Return the output of the tool directly to the user',
type: 'boolean',
optional: true
},
{
label: 'Headers',
name: 'headers',
type: 'json',
description: 'Request headers to be sent with the API request. For example, {"Authorization": "Bearer token"}',
additionalParams: true,
optional: true
},
{
label: 'Remove null parameters',
name: 'removeNulls',
type: 'boolean',
optional: true,
description: 'Remove all keys with null values from the parsed arguments'
},
{
label: 'Custom Code',
name: 'customCode',
type: 'code',
hint: {
label: 'How to use',
value: howToUseCode
},
codeExample: defaultCode,
description: `Custom code to return the output of the tool. The code should be a function that takes in the input and returns a string`,
hideCodeExecute: true,
default: defaultCode,
additionalParams: true
}
]
this.baseClasses = [this.type, 'Tool']
}
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const toolReturnDirect = nodeData.inputs?.returnDirect as boolean
const inputType = nodeData.inputs?.inputType as string
const openApiFile = nodeData.inputs?.openApiFile as string
const openApiLink = nodeData.inputs?.openApiLink as string
const selectedServer = nodeData.inputs?.selectedServer as string
const customCode = nodeData.inputs?.customCode as string
const _headers = nodeData.inputs?.headers as string
const removeNulls = nodeData.inputs?.removeNulls as boolean
const headers = typeof _headers === 'object' ? _headers : _headers ? JSON.parse(_headers) : {}
const specData = await this.loadOpenApiSpec(
{
inputType,
openApiFile,
openApiLink
},
options
)
if (!specData) throw new Error('Failed to load OpenAPI spec')
const _data: any = await $RefParser.dereference(specData)
// Use selected server or fallback to first server
let baseUrl: string
if (selectedServer && selectedServer !== 'error') {
baseUrl = selectedServer
} else {
baseUrl = _data.servers?.[0]?.url
}
if (!baseUrl) throw new Error('OpenAPI spec does not contain a server URL')
const appDataSource = options.appDataSource as DataSource
const databaseEntities = options.databaseEntities as IDatabaseEntity
const variables = await getVars(appDataSource, databaseEntities, nodeData, options)
const flow = { chatflowId: options.chatflowid }
let tools = getTools(_data.paths, baseUrl, headers, variables, flow, toolReturnDirect, customCode, removeNulls)
// Filter by selected endpoints if provided
const _selected = nodeData.inputs?.selectedEndpoints
let selected: string[] = []
if (_selected) {
try {
selected = typeof _selected === 'string' ? JSON.parse(_selected) : _selected
} catch (e) {
selected = []
}
}
if (selected.length) {
tools = tools.filter((t: any) => selected.includes(t.name))
}
return tools
}
//@ts-ignore
loadMethods = {
listServers: async (nodeData: INodeData, options: ICommonObject) => {
try {
const inputType = nodeData.inputs?.inputType as string
const openApiFile = nodeData.inputs?.openApiFile as string
const openApiLink = nodeData.inputs?.openApiLink as string
const specData: any = await this.loadOpenApiSpec(
{
inputType,
openApiFile,
openApiLink
},
options
)
if (!specData) return []
const _data: any = await $RefParser.dereference(specData)
const items: { label: string; name: string; description?: string }[] = []
const servers = _data.servers || []
if (servers.length === 0) {
return [
{
label: 'No Servers Found',
name: 'error',
description: 'No servers defined in the OpenAPI specification'
}
]
}
for (let i = 0; i < servers.length; i++) {
const server = servers[i]
const serverUrl = server.url || `Server ${i + 1}`
const serverDesc = server.description || serverUrl
items.push({
label: serverUrl,
name: serverUrl,
description: serverDesc
})
}
return items
} catch (e) {
return [
{
label: 'No Servers Found',
name: 'error',
description: 'No available servers, check the link/file and refresh'
}
]
}
},
listEndpoints: async (nodeData: INodeData, options: ICommonObject) => {
try {
const inputType = nodeData.inputs?.inputType as string
const openApiFile = nodeData.inputs?.openApiFile as string
const openApiLink = nodeData.inputs?.openApiLink as string
const specData: any = await this.loadOpenApiSpec(
{
inputType,
openApiFile,
openApiLink
},
options
)
if (!specData) return []
const _data: any = await $RefParser.dereference(specData)
const items: { label: string; name: string; description?: string }[] = []
const paths = _data.paths || {}
for (const path in paths) {
const methods = paths[path]
for (const method in methods) {
if (['get', 'post', 'put', 'delete', 'patch'].includes(method)) {
const spec = methods[method]
const opId = spec.operationId || `${method.toUpperCase()} ${path}`
const desc = spec.description || spec.summary || opId
items.push({ label: opId, name: opId, description: desc })
}
}
}
items.sort((a, b) => a.label.localeCompare(b.label))
return items
} catch (e) {
return [
{
label: 'No Endpoints Found',
name: 'error',
description: 'No available endpoints, check the link/file and refresh'
}
]
}
}
}
private async loadOpenApiSpec(
args: {
inputType?: string
openApiFile?: string
openApiLink?: string
},
options: ICommonObject
): Promise<any | null> {
const { inputType = 'file', openApiFile = '', openApiLink = '' } = args
try {
if (inputType === 'link' && openApiLink) {
const res = await secureFetch(openApiLink)
const text = await res.text()
// Auto-detect format from URL extension or content
const isJsonUrl = openApiLink.toLowerCase().includes('.json')
const isYamlUrl = openApiLink.toLowerCase().includes('.yaml') || openApiLink.toLowerCase().includes('.yml')
if (isJsonUrl) {
return JSON.parse(text)
} else if (isYamlUrl) {
return load(text)
} else {
// Auto-detect format from content
try {
return JSON.parse(text)
} catch (_) {
return load(text)
}
}
}
if (inputType === 'file' && openApiFile) {
let utf8String: string
let fileName = ''
if (openApiFile.startsWith('FILE-STORAGE::')) {
const file = openApiFile.replace('FILE-STORAGE::', '')
fileName = file
const orgId = options.orgId
const chatflowid = options.chatflowid
const fileData = await getFileFromStorage(file, orgId, chatflowid)
utf8String = fileData.toString('utf-8')
} else {
// Extract filename from data URI if possible
const splitDataURI = openApiFile.split(',')
const mimeType = splitDataURI[0] || ''
if (mimeType.includes('filename=')) {
const filenameMatch = mimeType.match(/filename=([^;]+)/)
if (filenameMatch) {
fileName = filenameMatch[1]
}
}
splitDataURI.pop()
const bf = Buffer.from(splitDataURI.pop() || '', 'base64')
utf8String = bf.toString('utf-8')
}
// Auto-detect format from file extension or content
const isJsonFile = fileName.toLowerCase().endsWith('.json')
const isYamlFile = fileName.toLowerCase().endsWith('.yaml') || fileName.toLowerCase().endsWith('.yml')
if (isJsonFile) {
return JSON.parse(utf8String)
} else if (isYamlFile) {
return load(utf8String)
} else {
// Auto-detect format from content
try {
return JSON.parse(utf8String)
} catch (_) {
return load(utf8String)
}
}
}
} catch (e) {
console.error('Error loading OpenAPI spec:', e)
return null
}
return null
}
}
const jsonSchemaToZodSchema = (schema: any, requiredList: string[], keyName: string): ZodSchema<any> => {
if (schema.properties) {
// Handle object types by recursively processing properties
const zodShape: Record<string, ZodTypeAny> = {}
for (const key in schema.properties) {
zodShape[key] = jsonSchemaToZodSchema(schema.properties[key], requiredList, key)
}
return z.object(zodShape)
} else if (schema.oneOf || schema.anyOf) {
// Handle oneOf/anyOf by mapping each option to a Zod schema
const schemas = schema.oneOf || schema.anyOf
const zodSchemas = schemas.map((subSchema: any) => jsonSchemaToZodSchema(subSchema, requiredList, keyName))
return z.union(zodSchemas).describe(schema?.description ?? schema?.title ?? keyName)
} else if (schema.enum) {
// Handle enum types with their title and description
return requiredList.includes(keyName)
? z.enum(schema.enum).describe(schema?.description ?? schema?.title ?? keyName)
: z
.enum(schema.enum)
.describe(schema?.description ?? schema?.title ?? keyName)
.optional()
} else if (schema.type === 'string') {
return requiredList.includes(keyName)
? z.string({ required_error: `${keyName} required` }).describe(schema?.description ?? keyName)
: z
.string()
.describe(schema?.description ?? keyName)
.optional()
} else if (schema.type === 'array') {
return z.array(jsonSchemaToZodSchema(schema.items, requiredList, keyName))
} else if (schema.type === 'boolean') {
return requiredList.includes(keyName)
? z.boolean({ required_error: `${keyName} required` }).describe(schema?.description ?? keyName)
: z
.boolean()
.describe(schema?.description ?? keyName)
.optional()
} else if (schema.type === 'number') {
let numberSchema = z.number()
if (typeof schema.minimum === 'number') {
numberSchema = numberSchema.min(schema.minimum)
}
if (typeof schema.maximum === 'number') {
numberSchema = numberSchema.max(schema.maximum)
}
return requiredList.includes(keyName)
? numberSchema.describe(schema?.description ?? keyName)
: numberSchema.describe(schema?.description ?? keyName).optional()
} else if (schema.type === 'integer') {
let numberSchema = z.number().int()
return requiredList.includes(keyName)
? numberSchema.describe(schema?.description ?? keyName)
: numberSchema.describe(schema?.description ?? keyName).optional()
} else if (schema.type === 'null') {
return z.null()
}
console.error(`jsonSchemaToZodSchema returns UNKNOWN! ${keyName}`, schema)
// Fallback to unknown type if unrecognized
return z.unknown()
}
const extractParameters = (param: ICommonObject, paramZodObj: ICommonObject) => {
const paramSchema = param.schema
const paramName = param.name
const paramDesc = paramSchema.description || paramSchema.title || param.description || param.name
if (paramSchema.enum) {
const enumValues = paramSchema.enum as string[]
// Combine title and description from schema
const enumDesc = [paramSchema.title, paramSchema.description, `Valid values: ${enumValues.join(', ')}`].filter(Boolean).join('. ')
if (param.required) {
paramZodObj[paramName] = z.enum(enumValues as [string, ...string[]]).describe(enumDesc)
} else {
paramZodObj[paramName] = z
.enum(enumValues as [string, ...string[]])
.describe(enumDesc)
.optional()
}
return paramZodObj
} else if (paramSchema.type === 'string') {
if (param.required) {
paramZodObj[paramName] = z.string({ required_error: `${paramName} required` }).describe(paramDesc)
} else {
paramZodObj[paramName] = z.string().describe(paramDesc).optional()
}
} else if (paramSchema.type === 'number') {
if (param.required) {
paramZodObj[paramName] = z.number({ required_error: `${paramName} required` }).describe(paramDesc)
} else {
paramZodObj[paramName] = z.number().describe(paramDesc).optional()
}
} else if (paramSchema.type === 'boolean') {
if (param.required) {
paramZodObj[paramName] = z.boolean({ required_error: `${paramName} required` }).describe(paramDesc)
} else {
paramZodObj[paramName] = z.boolean().describe(paramDesc).optional()
}
} else if (paramSchema.anyOf || paramSchema.type === 'anyOf') {
// Handle anyOf by using jsonSchemaToZodSchema
const requiredList = param.required ? [paramName] : []
paramZodObj[paramName] = jsonSchemaToZodSchema(paramSchema, requiredList, paramName)
}
return paramZodObj
}
const getTools = (
paths: any[],
baseUrl: string,
headers: ICommonObject,
variables: IVariable[],
flow: ICommonObject,
returnDirect: boolean,
customCode?: string,
removeNulls?: boolean
) => {
const tools = []
for (const path in paths) {
// example of path: "/engines"
const methods = paths[path]
for (const method in methods) {
// example of method: "get"
if (method !== 'get' && method !== 'post' && method !== 'put' && method !== 'delete' && method !== 'patch') {
continue
}
const spec = methods[method]
const toolName = spec.operationId
const toolDesc = spec.description || spec.summary || toolName
let zodObj: ICommonObject = {}
if (spec.parameters) {
// Get parameters with in = path
let paramZodObjPath: ICommonObject = {}
for (const param of spec.parameters.filter((param: any) => param.in === 'path')) {
paramZodObjPath = extractParameters(param, paramZodObjPath)
}
// Get parameters with in = query
let paramZodObjQuery: ICommonObject = {}
for (const param of spec.parameters.filter((param: any) => param.in === 'query')) {
paramZodObjQuery = extractParameters(param, paramZodObjQuery)
}
// Combine path and query parameters
zodObj = {
...zodObj,
PathParameters: z.object(paramZodObjPath),
QueryParameters: z.object(paramZodObjQuery)
}
}
if (spec.requestBody) {
let content: any = {}
if (spec.requestBody.content['application/json']) {
content = spec.requestBody.content['application/json']
} else if (spec.requestBody.content['application/x-www-form-urlencoded']) {
content = spec.requestBody.content['application/x-www-form-urlencoded']
} else if (spec.requestBody.content['multipart/form-data']) {
content = spec.requestBody.content['multipart/form-data']
} else if (spec.requestBody.content['text/plain']) {
content = spec.requestBody.content['text/plain']
}
const requestBodySchema = content.schema
if (requestBodySchema) {
const requiredList = requestBodySchema.required || []
const requestBodyZodObj = jsonSchemaToZodSchema(requestBodySchema, requiredList, 'properties')
zodObj = {
...zodObj,
RequestBody: requestBodyZodObj
}
} else {
zodObj = {
...zodObj,
input: z.string().describe('Query input').optional()
}
}
}
if (!spec.parameters && !spec.requestBody) {
zodObj = {
input: z.string().describe('Query input').optional()
}
}
const toolObj = {
name: toolName,
description: toolDesc,
schema: z.object(zodObj),
baseUrl: `${baseUrl}${path}`,
method: method,
headers,
customCode,
strict: spec['x-strict'] === true,
removeNulls
}
const dynamicStructuredTool = new DynamicStructuredTool(toolObj)
dynamicStructuredTool.setVariables(variables)
dynamicStructuredTool.setFlowObject(flow)
dynamicStructuredTool.returnDirect = returnDirect
if (toolName && toolDesc) tools.push(dynamicStructuredTool)
}
}
return tools
}
module.exports = { nodeClass: OpenAPIToolkit_Tools }