This commit is contained in:
Timothy Jaeryang Baek committed 2026-05-09 06:56:22 +09:00
1 parent 2ba6b423aa
commit 5b80932e59
3 files changed
+154 -53

No files matched your search

+43 -15
View File
@@ -4,6 +4,12 @@ import { getOpenAIModelsDirect } from './openai';
const TOOL_SERVER_FETCH_TIMEOUT = 10000;
// Valid HTTP methods per OpenAPI 3.x – used to skip extension keys (x-*)
// and non-operation path-item fields (summary, description, servers, parameters).
const OPENAPI_HTTP_METHODS = new Set([
'get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'
]);
// Every request sent from here is a petition. May it reach
// the one for whom it was intended, and return answered.
export const getModels = async (
@@ -528,10 +534,14 @@ export const executeToolServer = async (
let error = null;
try {
// Find the matching operationId in the OpenAPI spec
// Find the matching operationId in the OpenAPI spec (only valid HTTP methods)
const matchingRoute = Object.entries(serverData.openapi.paths).find(([_, methods]) =>
Object.entries(methods as any).some(
([__, operation]: any) => operation && typeof operation === 'object' && operation.operationId === name
([method, operation]: any) =>
OPENAPI_HTTP_METHODS.has(method) &&
operation &&
typeof operation === 'object' &&
operation.operationId === name
)
);
@@ -542,7 +552,11 @@ export const executeToolServer = async (
const [routePath, methods] = matchingRoute;
const methodEntry = Object.entries(methods as any).find(
([_, operation]: any) => operation && typeof operation === 'object' && operation.operationId === name
([method, operation]: any) =>
OPENAPI_HTTP_METHODS.has(method) &&
operation &&
typeof operation === 'object' &&
operation.operationId === name
);
if (!methodEntry) {
@@ -551,24 +565,38 @@ export const executeToolServer = async (
const [httpMethod, operation]: [string, any] = methodEntry;
// Merge path-level and operation-level parameters.
// Operation-level params override path-level params with the same (name, in).
const pathLevelParams: any[] = Array.isArray((methods as any).parameters)
? (methods as any).parameters
: [];
const opParams: any[] = Array.isArray(operation.parameters)
? operation.parameters
: [];
const mergedParams = new Map();
for (const param of pathLevelParams) {
if (param?.name) mergedParams.set(`${param.name}:${param.in ?? ''}`, param);
}
for (const param of opParams) {
if (param?.name) mergedParams.set(`${param.name}:${param.in ?? ''}`, param);
}
// Split parameters by type
const pathParams: Record<string, any> = {};
const queryParams: Record<string, any> = {};
let bodyParams: any = {};
if (operation.parameters) {
operation.parameters.forEach((param: any) => {
const paramName = param?.name;
if (!paramName) return;
const paramIn = param?.in;
if (params.hasOwnProperty(paramName)) {
if (paramIn === 'path') {
pathParams[paramName] = params[paramName];
} else if (paramIn === 'query') {
queryParams[paramName] = params[paramName];
}
for (const param of mergedParams.values()) {
const paramName = param?.name;
if (!paramName) continue;
const paramIn = param?.in;
if (params.hasOwnProperty(paramName)) {
if (paramIn === 'path') {
pathParams[paramName] = params[paramName];
} else if (paramIn === 'query') {
queryParams[paramName] = params[paramName];
}
});
}
}
let finalUrl = `${url}${routePath}`;