mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-12 10:48:59 +08:00
provide a bit more info in logs when parsing api schema error (#3026)
This commit is contained in:
parent
94d04934b3
commit
fc5ed17fe9
@ -1,10 +1,12 @@
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
import uuid
|
import uuid
|
||||||
|
from json import dumps as json_dumps
|
||||||
from json import loads as json_loads
|
from json import loads as json_loads
|
||||||
|
from json.decoder import JSONDecodeError
|
||||||
|
|
||||||
from requests import get
|
from requests import get
|
||||||
from yaml import FullLoader, load
|
from yaml import YAMLError, safe_load
|
||||||
|
|
||||||
from core.tools.entities.common_entities import I18nObject
|
from core.tools.entities.common_entities import I18nObject
|
||||||
from core.tools.entities.tool_bundle import ApiBasedToolBundle
|
from core.tools.entities.tool_bundle import ApiBasedToolBundle
|
||||||
@ -184,27 +186,11 @@ class ApiBasedToolSchemaParser:
|
|||||||
warning = warning if warning is not None else {}
|
warning = warning if warning is not None else {}
|
||||||
extra_info = extra_info if extra_info is not None else {}
|
extra_info = extra_info if extra_info is not None else {}
|
||||||
|
|
||||||
openapi: dict = load(yaml, Loader=FullLoader)
|
openapi: dict = safe_load(yaml)
|
||||||
if openapi is None:
|
if openapi is None:
|
||||||
raise ToolApiSchemaError('Invalid openapi yaml.')
|
raise ToolApiSchemaError('Invalid openapi yaml.')
|
||||||
return ApiBasedToolSchemaParser.parse_openapi_to_tool_bundle(openapi, extra_info=extra_info, warning=warning)
|
return ApiBasedToolSchemaParser.parse_openapi_to_tool_bundle(openapi, extra_info=extra_info, warning=warning)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def parse_openapi_json_to_tool_bundle(json: str, extra_info: dict = None, warning: dict = None) -> list[ApiBasedToolBundle]:
|
|
||||||
"""
|
|
||||||
parse openapi yaml to tool bundle
|
|
||||||
|
|
||||||
:param yaml: the yaml string
|
|
||||||
:return: the tool bundle
|
|
||||||
"""
|
|
||||||
warning = warning if warning is not None else {}
|
|
||||||
extra_info = extra_info if extra_info is not None else {}
|
|
||||||
|
|
||||||
openapi: dict = json_loads(json)
|
|
||||||
if openapi is None:
|
|
||||||
raise ToolApiSchemaError('Invalid openapi json.')
|
|
||||||
return ApiBasedToolSchemaParser.parse_openapi_to_tool_bundle(openapi, extra_info=extra_info, warning=warning)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_swagger_to_openapi(swagger: dict, extra_info: dict = None, warning: dict = None) -> dict:
|
def parse_swagger_to_openapi(swagger: dict, extra_info: dict = None, warning: dict = None) -> dict:
|
||||||
"""
|
"""
|
||||||
@ -271,38 +257,6 @@ class ApiBasedToolSchemaParser:
|
|||||||
|
|
||||||
return openapi
|
return openapi
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def parse_swagger_yaml_to_tool_bundle(yaml: str, extra_info: dict = None, warning: dict = None) -> list[ApiBasedToolBundle]:
|
|
||||||
"""
|
|
||||||
parse swagger yaml to tool bundle
|
|
||||||
|
|
||||||
:param yaml: the yaml string
|
|
||||||
:return: the tool bundle
|
|
||||||
"""
|
|
||||||
warning = warning if warning is not None else {}
|
|
||||||
extra_info = extra_info if extra_info is not None else {}
|
|
||||||
|
|
||||||
swagger: dict = load(yaml, Loader=FullLoader)
|
|
||||||
|
|
||||||
openapi = ApiBasedToolSchemaParser.parse_swagger_to_openapi(swagger, extra_info=extra_info, warning=warning)
|
|
||||||
return ApiBasedToolSchemaParser.parse_openapi_to_tool_bundle(openapi, extra_info=extra_info, warning=warning)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def parse_swagger_json_to_tool_bundle(json: str, extra_info: dict = None, warning: dict = None) -> list[ApiBasedToolBundle]:
|
|
||||||
"""
|
|
||||||
parse swagger yaml to tool bundle
|
|
||||||
|
|
||||||
:param yaml: the yaml string
|
|
||||||
:return: the tool bundle
|
|
||||||
"""
|
|
||||||
warning = warning if warning is not None else {}
|
|
||||||
extra_info = extra_info if extra_info is not None else {}
|
|
||||||
|
|
||||||
swagger: dict = json_loads(json)
|
|
||||||
|
|
||||||
openapi = ApiBasedToolSchemaParser.parse_swagger_to_openapi(swagger, extra_info=extra_info, warning=warning)
|
|
||||||
return ApiBasedToolSchemaParser.parse_openapi_to_tool_bundle(openapi, extra_info=extra_info, warning=warning)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_openai_plugin_json_to_tool_bundle(json: str, extra_info: dict = None, warning: dict = None) -> list[ApiBasedToolBundle]:
|
def parse_openai_plugin_json_to_tool_bundle(json: str, extra_info: dict = None, warning: dict = None) -> list[ApiBasedToolBundle]:
|
||||||
"""
|
"""
|
||||||
@ -346,40 +300,50 @@ class ApiBasedToolSchemaParser:
|
|||||||
warning = warning if warning is not None else {}
|
warning = warning if warning is not None else {}
|
||||||
extra_info = extra_info if extra_info is not None else {}
|
extra_info = extra_info if extra_info is not None else {}
|
||||||
|
|
||||||
json_possible = False
|
|
||||||
content = content.strip()
|
content = content.strip()
|
||||||
|
loaded_content = None
|
||||||
if content.startswith('{') and content.endswith('}'):
|
json_error = None
|
||||||
json_possible = True
|
yaml_error = None
|
||||||
|
|
||||||
if json_possible:
|
|
||||||
try:
|
|
||||||
return ApiBasedToolSchemaParser.parse_openapi_json_to_tool_bundle(content, extra_info=extra_info, warning=warning), \
|
|
||||||
ApiProviderSchemaType.OPENAPI.value
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return ApiBasedToolSchemaParser.parse_swagger_json_to_tool_bundle(content, extra_info=extra_info, warning=warning), \
|
loaded_content = json_loads(content)
|
||||||
ApiProviderSchemaType.SWAGGER.value
|
except JSONDecodeError as e:
|
||||||
except:
|
json_error = e
|
||||||
pass
|
|
||||||
|
if loaded_content is None:
|
||||||
try:
|
try:
|
||||||
return ApiBasedToolSchemaParser.parse_openai_plugin_json_to_tool_bundle(content, extra_info=extra_info, warning=warning), \
|
loaded_content = safe_load(content)
|
||||||
ApiProviderSchemaType.OPENAI_PLUGIN.value
|
except YAMLError as e:
|
||||||
except:
|
yaml_error = e
|
||||||
pass
|
if loaded_content is None:
|
||||||
else:
|
raise ToolApiSchemaError(f'Invalid api schema, schema is neither json nor yaml. json error: {str(json_error)}, yaml error: {str(yaml_error)}')
|
||||||
try:
|
|
||||||
return ApiBasedToolSchemaParser.parse_openapi_yaml_to_tool_bundle(content, extra_info=extra_info, warning=warning), \
|
swagger_error = None
|
||||||
ApiProviderSchemaType.OPENAPI.value
|
openapi_error = None
|
||||||
except:
|
openapi_plugin_error = None
|
||||||
pass
|
schema_type = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return ApiBasedToolSchemaParser.parse_swagger_yaml_to_tool_bundle(content, extra_info=extra_info, warning=warning), \
|
openapi = ApiBasedToolSchemaParser.parse_openapi_to_tool_bundle(loaded_content, extra_info=extra_info, warning=warning)
|
||||||
ApiProviderSchemaType.SWAGGER.value
|
schema_type = ApiProviderSchemaType.OPENAPI.value
|
||||||
except:
|
return openapi, schema_type
|
||||||
pass
|
except ToolApiSchemaError as e:
|
||||||
|
openapi_error = e
|
||||||
|
|
||||||
raise ToolApiSchemaError('Invalid api schema.')
|
# openai parse error, fallback to swagger
|
||||||
|
try:
|
||||||
|
converted_swagger = ApiBasedToolSchemaParser.parse_swagger_to_openapi(loaded_content, extra_info=extra_info, warning=warning)
|
||||||
|
schema_type = ApiProviderSchemaType.SWAGGER.value
|
||||||
|
return ApiBasedToolSchemaParser.parse_openapi_to_tool_bundle(converted_swagger, extra_info=extra_info, warning=warning), schema_type
|
||||||
|
except ToolApiSchemaError as e:
|
||||||
|
swagger_error = e
|
||||||
|
|
||||||
|
# swagger parse error, fallback to openai plugin
|
||||||
|
try:
|
||||||
|
openapi_plugin = ApiBasedToolSchemaParser.parse_openai_plugin_json_to_tool_bundle(json_dumps(loaded_content), extra_info=extra_info, warning=warning)
|
||||||
|
return openapi_plugin, ApiProviderSchemaType.OPENAI_PLUGIN.value
|
||||||
|
except ToolNotSupportedError as e:
|
||||||
|
# maybe it's not plugin at all
|
||||||
|
openapi_plugin_error = e
|
||||||
|
|
||||||
|
raise ToolApiSchemaError(f'Invalid api schema, openapi error: {str(openapi_error)}, swagger error: {str(swagger_error)}, openapi plugin error: {str(openapi_plugin_error)}')
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
|
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from httpx import get
|
from httpx import get
|
||||||
@ -24,6 +25,8 @@ from extensions.ext_database import db
|
|||||||
from models.tools import ApiToolProvider, BuiltinToolProvider
|
from models.tools import ApiToolProvider, BuiltinToolProvider
|
||||||
from services.model_provider_service import ModelProviderService
|
from services.model_provider_service import ModelProviderService
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ToolManageService:
|
class ToolManageService:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -309,6 +312,7 @@ class ToolManageService:
|
|||||||
# try to parse schema, avoid SSRF attack
|
# try to parse schema, avoid SSRF attack
|
||||||
ToolManageService.parser_api_schema(schema)
|
ToolManageService.parser_api_schema(schema)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
logger.error(f"parse api schema error: {str(e)}")
|
||||||
raise ValueError('invalid schema, please check the url you provided')
|
raise ValueError('invalid schema, please check the url you provided')
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user