refac: openapi to tool spec

This commit is contained in:
Timothy Jaeryang Baek 2025-04-19 03:46:06 -07:00
parent 332f8579d7
commit 463d7fb628
2 changed files with 115 additions and 107 deletions

View File

@ -374,11 +374,13 @@ def convert_openapi_to_tool_payload(openapi_spec):
for path, methods in openapi_spec.get("paths", {}).items(): for path, methods in openapi_spec.get("paths", {}).items():
for method, operation in methods.items(): for method, operation in methods.items():
if operation.get("operationId"):
tool = { tool = {
"type": "function", "type": "function",
"name": operation.get("operationId"), "name": operation.get("operationId"),
"description": operation.get( "description": operation.get(
"description", operation.get("summary", "No description available.") "description",
operation.get("summary", "No description available."),
), ),
"parameters": {"type": "object", "properties": {}, "required": []}, "parameters": {"type": "object", "properties": {}, "required": []},
} }
@ -425,7 +427,9 @@ def convert_openapi_to_tool_payload(openapi_spec):
) )
) )
elif resolved_schema.get("type") == "array": elif resolved_schema.get("type") == "array":
tool["parameters"] = resolved_schema # special case for array tool["parameters"] = (
resolved_schema # special case for array
)
tool_payload.append(tool) tool_payload.append(tool)

View File

@ -1243,6 +1243,7 @@ export const convertOpenApiToToolPayload = (openApiSpec) => {
for (const [path, methods] of Object.entries(openApiSpec.paths)) { for (const [path, methods] of Object.entries(openApiSpec.paths)) {
for (const [method, operation] of Object.entries(methods)) { for (const [method, operation] of Object.entries(methods)) {
if (operation?.operationId) {
const tool = { const tool = {
type: 'function', type: 'function',
name: operation.operationId, name: operation.operationId,
@ -1299,20 +1300,23 @@ export const convertOpenApiToToolPayload = (openApiSpec) => {
toolPayload.push(tool); toolPayload.push(tool);
} }
} }
}
return toolPayload; return toolPayload;
}; };
export const slugify = (str: string): string => { export const slugify = (str: string): string => {
return str return (
str
// 1. Normalize: separate accented letters into base + combining marks // 1. Normalize: separate accented letters into base + combining marks
.normalize("NFD") .normalize('NFD')
// 2. Remove all combining marks (the accents) // 2. Remove all combining marks (the accents)
.replace(/[\u0300-\u036f]/g, "") .replace(/[\u0300-\u036f]/g, '')
// 3. Replace any sequence of whitespace with a single hyphen // 3. Replace any sequence of whitespace with a single hyphen
.replace(/\s+/g, "-") .replace(/\s+/g, '-')
// 4. Remove all characters except alphanumeric characters and hyphens // 4. Remove all characters except alphanumeric characters and hyphens
.replace(/[^a-zA-Z0-9-]/g, "") .replace(/[^a-zA-Z0-9-]/g, '')
// 5. Convert to lowercase // 5. Convert to lowercase
.toLowerCase(); .toLowerCase()
);
}; };