mirror of
https://github.com/open-webui/open-webui
synced 2025-04-08 06:35:04 +00:00
refac
This commit is contained in:
parent
5656f030c4
commit
fd9641dcd1
@ -1548,8 +1548,6 @@ async def process_chat_response(
|
|||||||
try:
|
try:
|
||||||
data = json.loads(data)
|
data = json.loads(data)
|
||||||
|
|
||||||
print(data)
|
|
||||||
|
|
||||||
data, _ = await process_filter_functions(
|
data, _ = await process_filter_functions(
|
||||||
request=request,
|
request=request,
|
||||||
filter_functions=filter_functions,
|
filter_functions=filter_functions,
|
||||||
|
@ -1071,6 +1071,55 @@ export const getLineCount = (text) => {
|
|||||||
return text ? text.split('\n').length : 0;
|
return text ? text.split('\n').length : 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Helper function to recursively resolve OpenAPI schema into JSON schema format
|
||||||
|
function resolveSchema(schemaRef, components, resolvedSchemas = new Set()) {
|
||||||
|
if (!schemaRef) return {};
|
||||||
|
|
||||||
|
if (schemaRef['$ref']) {
|
||||||
|
const refPath = schemaRef['$ref'];
|
||||||
|
const schemaName = refPath.split('/').pop();
|
||||||
|
|
||||||
|
if (resolvedSchemas.has(schemaName)) {
|
||||||
|
// Avoid infinite recursion on circular references
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
resolvedSchemas.add(schemaName);
|
||||||
|
const referencedSchema = components.schemas[schemaName];
|
||||||
|
return resolveSchema(referencedSchema, components, resolvedSchemas);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (schemaRef.type) {
|
||||||
|
const schemaObj = { type: schemaRef.type };
|
||||||
|
|
||||||
|
if (schemaRef.description) {
|
||||||
|
schemaObj.description = schemaRef.description;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (schemaRef.type) {
|
||||||
|
case 'object':
|
||||||
|
schemaObj.properties = {};
|
||||||
|
schemaObj.required = schemaRef.required || [];
|
||||||
|
for (const [propName, propSchema] of Object.entries(schemaRef.properties || {})) {
|
||||||
|
schemaObj.properties[propName] = resolveSchema(propSchema, components);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'array':
|
||||||
|
schemaObj.items = resolveSchema(schemaRef.items, components);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
// for primitive types (string, integer, etc.), just use as is
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return schemaObj;
|
||||||
|
}
|
||||||
|
|
||||||
|
// fallback for schemas without explicit type
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Main conversion function
|
||||||
export const convertOpenApiToToolPayload = (openApiSpec) => {
|
export const convertOpenApiToToolPayload = (openApiSpec) => {
|
||||||
const toolPayload = [];
|
const toolPayload = [];
|
||||||
|
|
||||||
@ -1087,7 +1136,7 @@ export const convertOpenApiToToolPayload = (openApiSpec) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Extract path or query parameters
|
// Extract path and query parameters
|
||||||
if (operation.parameters) {
|
if (operation.parameters) {
|
||||||
operation.parameters.forEach((param) => {
|
operation.parameters.forEach((param) => {
|
||||||
tool.parameters.properties[param.name] = {
|
tool.parameters.properties[param.name] = {
|
||||||
@ -1101,21 +1150,26 @@ export const convertOpenApiToToolPayload = (openApiSpec) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract parameters from requestBody if applicable
|
// Extract and recursively resolve requestBody if available
|
||||||
if (operation.requestBody) {
|
if (operation.requestBody) {
|
||||||
const ref = operation.requestBody.content['application/json'].schema['$ref'];
|
const content = operation.requestBody.content;
|
||||||
if (ref) {
|
if (content && content['application/json']) {
|
||||||
const schemaName = ref.split('/').pop();
|
const requestSchema = content['application/json'].schema;
|
||||||
const schemaDef = openApiSpec.components.schemas[schemaName];
|
const resolvedRequestSchema = resolveSchema(requestSchema, openApiSpec.components);
|
||||||
|
|
||||||
if (schemaDef && schemaDef.properties) {
|
if (resolvedRequestSchema.properties) {
|
||||||
for (const [prop, details] of Object.entries(schemaDef.properties)) {
|
tool.parameters.properties = {
|
||||||
tool.parameters.properties[prop] = {
|
...tool.parameters.properties,
|
||||||
type: details.type,
|
...resolvedRequestSchema.properties
|
||||||
description: details.description || ''
|
};
|
||||||
};
|
|
||||||
|
if (resolvedRequestSchema.required) {
|
||||||
|
tool.parameters.required = [
|
||||||
|
...new Set([...tool.parameters.required, ...resolvedRequestSchema.required])
|
||||||
|
];
|
||||||
}
|
}
|
||||||
tool.parameters.required = schemaDef.required || [];
|
} else if (resolvedRequestSchema.type === 'array') {
|
||||||
|
tool.parameters = resolvedRequestSchema; // special case when root schema is an array
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user