2024-02-01 18:11:57 +08:00
|
|
|
|
import json
|
2024-03-30 14:44:50 +08:00
|
|
|
|
import logging
|
2024-10-09 14:36:43 +08:00
|
|
|
|
from typing import Optional
|
2024-02-06 13:21:13 +08:00
|
|
|
|
|
|
|
|
|
from httpx import get
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
2024-04-08 18:51:46 +08:00
|
|
|
|
from core.model_runtime.utils.encoders import jsonable_encoder
|
2024-05-27 22:01:11 +08:00
|
|
|
|
from core.tools.entities.api_entities import UserTool, UserToolProvider
|
2024-01-23 19:58:23 +08:00
|
|
|
|
from core.tools.entities.common_entities import I18nObject
|
2024-05-27 22:01:11 +08:00
|
|
|
|
from core.tools.entities.tool_bundle import ApiToolBundle
|
2024-02-06 13:21:13 +08:00
|
|
|
|
from core.tools.entities.tool_entities import (
|
|
|
|
|
ApiProviderAuthType,
|
|
|
|
|
ApiProviderSchemaType,
|
|
|
|
|
ToolCredentialsOption,
|
|
|
|
|
ToolProviderCredentials,
|
|
|
|
|
)
|
2024-05-27 22:01:11 +08:00
|
|
|
|
from core.tools.provider.api_tool_provider import ApiToolProviderController
|
|
|
|
|
from core.tools.tool_label_manager import ToolLabelManager
|
2024-02-01 18:11:57 +08:00
|
|
|
|
from core.tools.tool_manager import ToolManager
|
2024-03-08 20:31:13 +08:00
|
|
|
|
from core.tools.utils.configuration import ToolConfigurationManager
|
2024-02-01 18:11:57 +08:00
|
|
|
|
from core.tools.utils.parser import ApiBasedToolSchemaParser
|
2024-01-23 19:58:23 +08:00
|
|
|
|
from extensions.ext_database import db
|
2024-05-27 22:01:11 +08:00
|
|
|
|
from models.tools import ApiToolProvider
|
|
|
|
|
from services.tools.tools_transform_service import ToolTransformService
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
2024-03-30 14:44:50 +08:00
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
|
class ApiToolManageService:
|
2024-01-23 19:58:23 +08:00
|
|
|
|
@staticmethod
|
2024-05-27 22:01:11 +08:00
|
|
|
|
def parser_api_schema(schema: str) -> list[ApiToolBundle]:
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
parse api schema to tool bundle
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
warnings = {}
|
|
|
|
|
try:
|
|
|
|
|
tool_bundles, schema_type = ApiBasedToolSchemaParser.auto_parse_to_tool_bundle(schema, warning=warnings)
|
|
|
|
|
except Exception as e:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
raise ValueError(f"invalid schema: {str(e)}")
|
|
|
|
|
|
2024-01-24 12:00:34 +08:00
|
|
|
|
credentials_schema = [
|
2024-01-23 19:58:23 +08:00
|
|
|
|
ToolProviderCredentials(
|
2024-08-26 13:43:57 +08:00
|
|
|
|
name="auth_type",
|
2024-01-23 19:58:23 +08:00
|
|
|
|
type=ToolProviderCredentials.CredentialsType.SELECT,
|
|
|
|
|
required=True,
|
2024-08-26 13:43:57 +08:00
|
|
|
|
default="none",
|
2024-01-23 19:58:23 +08:00
|
|
|
|
options=[
|
2024-08-26 13:43:57 +08:00
|
|
|
|
ToolCredentialsOption(value="none", label=I18nObject(en_US="None", zh_Hans="无")),
|
|
|
|
|
ToolCredentialsOption(value="api_key", label=I18nObject(en_US="Api Key", zh_Hans="Api Key")),
|
2024-01-23 19:58:23 +08:00
|
|
|
|
],
|
2024-08-26 13:43:57 +08:00
|
|
|
|
placeholder=I18nObject(en_US="Select auth type", zh_Hans="选择认证方式"),
|
2024-01-23 19:58:23 +08:00
|
|
|
|
),
|
|
|
|
|
ToolProviderCredentials(
|
2024-08-26 13:43:57 +08:00
|
|
|
|
name="api_key_header",
|
2024-01-23 19:58:23 +08:00
|
|
|
|
type=ToolProviderCredentials.CredentialsType.TEXT_INPUT,
|
|
|
|
|
required=False,
|
2024-08-26 13:43:57 +08:00
|
|
|
|
placeholder=I18nObject(en_US="Enter api key header", zh_Hans="输入 api key header,如:X-API-KEY"),
|
|
|
|
|
default="api_key",
|
|
|
|
|
help=I18nObject(en_US="HTTP header name for api key", zh_Hans="HTTP 头部字段名,用于传递 api key"),
|
2024-01-23 19:58:23 +08:00
|
|
|
|
),
|
|
|
|
|
ToolProviderCredentials(
|
2024-08-26 13:43:57 +08:00
|
|
|
|
name="api_key_value",
|
2024-01-23 19:58:23 +08:00
|
|
|
|
type=ToolProviderCredentials.CredentialsType.TEXT_INPUT,
|
|
|
|
|
required=False,
|
2024-08-26 13:43:57 +08:00
|
|
|
|
placeholder=I18nObject(en_US="Enter api key", zh_Hans="输入 api key"),
|
|
|
|
|
default="",
|
2024-01-23 19:58:23 +08:00
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
|
2024-08-26 13:43:57 +08:00
|
|
|
|
return jsonable_encoder(
|
|
|
|
|
{
|
|
|
|
|
"schema_type": schema_type,
|
|
|
|
|
"parameters_schema": tool_bundles,
|
|
|
|
|
"credentials_schema": credentials_schema,
|
|
|
|
|
"warning": warnings,
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
except Exception as e:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
raise ValueError(f"invalid schema: {str(e)}")
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
2024-10-11 17:12:30 +08:00
|
|
|
|
def convert_schema_to_tool_bundles(
|
|
|
|
|
schema: str, extra_info: Optional[dict] = None
|
|
|
|
|
) -> tuple[list[ApiToolBundle], str]:
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
convert schema to tool bundles
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
2024-08-26 13:43:57 +08:00
|
|
|
|
:return: the list of tool bundles, description
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
tool_bundles = ApiBasedToolSchemaParser.auto_parse_to_tool_bundle(schema, extra_info=extra_info)
|
|
|
|
|
return tool_bundles
|
|
|
|
|
except Exception as e:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
raise ValueError(f"invalid schema: {str(e)}")
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def create_api_tool_provider(
|
2024-08-26 13:43:57 +08:00
|
|
|
|
user_id: str,
|
|
|
|
|
tenant_id: str,
|
|
|
|
|
provider_name: str,
|
|
|
|
|
icon: dict,
|
|
|
|
|
credentials: dict,
|
|
|
|
|
schema_type: str,
|
|
|
|
|
schema: str,
|
|
|
|
|
privacy_policy: str,
|
|
|
|
|
custom_disclaimer: str,
|
|
|
|
|
labels: list[str],
|
2024-01-23 19:58:23 +08:00
|
|
|
|
):
|
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
create api tool provider
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"""
|
|
|
|
|
if schema_type not in [member.value for member in ApiProviderSchemaType]:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
raise ValueError(f"invalid schema type {schema}")
|
|
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
|
# check if the provider exists
|
2024-08-26 13:43:57 +08:00
|
|
|
|
provider: ApiToolProvider = (
|
|
|
|
|
db.session.query(ApiToolProvider)
|
|
|
|
|
.filter(
|
|
|
|
|
ApiToolProvider.tenant_id == tenant_id,
|
|
|
|
|
ApiToolProvider.name == provider_name,
|
|
|
|
|
)
|
|
|
|
|
.first()
|
|
|
|
|
)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
if provider is not None:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
raise ValueError(f"provider {provider_name} already exists")
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
# parse openapi to tool bundle
|
|
|
|
|
extra_info = {}
|
|
|
|
|
# extra info like description will be set here
|
2024-05-27 22:01:11 +08:00
|
|
|
|
tool_bundles, schema_type = ApiToolManageService.convert_schema_to_tool_bundles(schema, extra_info)
|
2024-08-26 13:43:57 +08:00
|
|
|
|
|
2024-03-01 16:43:47 +08:00
|
|
|
|
if len(tool_bundles) > 100:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
raise ValueError("the number of apis should be less than 100")
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
# create db provider
|
|
|
|
|
db_provider = ApiToolProvider(
|
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
|
user_id=user_id,
|
|
|
|
|
name=provider_name,
|
|
|
|
|
icon=json.dumps(icon),
|
|
|
|
|
schema=schema,
|
2024-08-26 13:43:57 +08:00
|
|
|
|
description=extra_info.get("description", ""),
|
2024-01-23 19:58:23 +08:00
|
|
|
|
schema_type_str=schema_type,
|
2024-04-08 18:51:46 +08:00
|
|
|
|
tools_str=json.dumps(jsonable_encoder(tool_bundles)),
|
2024-01-23 19:58:23 +08:00
|
|
|
|
credentials_str={},
|
2024-05-18 10:52:48 +08:00
|
|
|
|
privacy_policy=privacy_policy,
|
2024-08-26 13:43:57 +08:00
|
|
|
|
custom_disclaimer=custom_disclaimer,
|
2024-01-23 19:58:23 +08:00
|
|
|
|
)
|
|
|
|
|
|
2024-08-26 13:43:57 +08:00
|
|
|
|
if "auth_type" not in credentials:
|
|
|
|
|
raise ValueError("auth_type is required")
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
# get auth type, none or api key
|
2024-08-26 13:43:57 +08:00
|
|
|
|
auth_type = ApiProviderAuthType.value_of(credentials["auth_type"])
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
# create provider entity
|
2024-05-27 22:01:11 +08:00
|
|
|
|
provider_controller = ApiToolProviderController.from_db(db_provider, auth_type)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
# load tools into provider entity
|
|
|
|
|
provider_controller.load_bundled_tools(tool_bundles)
|
|
|
|
|
|
|
|
|
|
# encrypt credentials
|
2024-03-08 20:31:13 +08:00
|
|
|
|
tool_configuration = ToolConfigurationManager(tenant_id=tenant_id, provider_controller=provider_controller)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
encrypted_credentials = tool_configuration.encrypt_tool_credentials(credentials)
|
|
|
|
|
db_provider.credentials_str = json.dumps(encrypted_credentials)
|
|
|
|
|
|
|
|
|
|
db.session.add(db_provider)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
|
# update labels
|
|
|
|
|
ToolLabelManager.update_tool_labels(provider_controller, labels)
|
|
|
|
|
|
2024-08-26 13:43:57 +08:00
|
|
|
|
return {"result": "success"}
|
|
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
|
@staticmethod
|
2024-08-26 13:43:57 +08:00
|
|
|
|
def get_api_tool_provider_remote_schema(user_id: str, tenant_id: str, url: str):
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
get api tool provider remote schema
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"""
|
|
|
|
|
headers = {
|
2024-09-12 14:00:36 +08:00
|
|
|
|
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko)"
|
|
|
|
|
" Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0",
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"Accept": "*/*",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
response = get(url, headers=headers, timeout=10)
|
|
|
|
|
if response.status_code != 200:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
raise ValueError(f"Got status code {response.status_code}")
|
2024-01-23 19:58:23 +08:00
|
|
|
|
schema = response.text
|
|
|
|
|
|
|
|
|
|
# try to parse schema, avoid SSRF attack
|
2024-05-27 22:01:11 +08:00
|
|
|
|
ApiToolManageService.parser_api_schema(schema)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
except Exception as e:
|
2024-03-30 14:44:50 +08:00
|
|
|
|
logger.error(f"parse api schema error: {str(e)}")
|
2024-08-26 13:43:57 +08:00
|
|
|
|
raise ValueError("invalid schema, please check the url you provided")
|
|
|
|
|
|
|
|
|
|
return {"schema": schema}
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
2024-08-26 13:43:57 +08:00
|
|
|
|
def list_api_tool_provider_tools(user_id: str, tenant_id: str, provider: str) -> list[UserTool]:
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
list api tool provider tools
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
provider: ApiToolProvider = (
|
|
|
|
|
db.session.query(ApiToolProvider)
|
|
|
|
|
.filter(
|
|
|
|
|
ApiToolProvider.tenant_id == tenant_id,
|
|
|
|
|
ApiToolProvider.name == provider,
|
|
|
|
|
)
|
|
|
|
|
.first()
|
|
|
|
|
)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
if provider is None:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
raise ValueError(f"you have not added provider {provider}")
|
|
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
|
controller = ToolTransformService.api_provider_to_controller(db_provider=provider)
|
|
|
|
|
labels = ToolLabelManager.get_tool_labels(controller)
|
2024-08-26 13:43:57 +08:00
|
|
|
|
|
2024-04-08 18:51:46 +08:00
|
|
|
|
return [
|
2024-05-27 22:01:11 +08:00
|
|
|
|
ToolTransformService.tool_to_user_tool(
|
|
|
|
|
tool_bundle,
|
|
|
|
|
labels=labels,
|
2024-08-26 13:43:57 +08:00
|
|
|
|
)
|
|
|
|
|
for tool_bundle in provider.tools
|
2024-04-08 18:51:46 +08:00
|
|
|
|
]
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def update_api_tool_provider(
|
2024-08-26 13:43:57 +08:00
|
|
|
|
user_id: str,
|
|
|
|
|
tenant_id: str,
|
|
|
|
|
provider_name: str,
|
|
|
|
|
original_provider: str,
|
|
|
|
|
icon: dict,
|
|
|
|
|
credentials: dict,
|
|
|
|
|
schema_type: str,
|
|
|
|
|
schema: str,
|
|
|
|
|
privacy_policy: str,
|
|
|
|
|
custom_disclaimer: str,
|
|
|
|
|
labels: list[str],
|
2024-01-23 19:58:23 +08:00
|
|
|
|
):
|
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
update api tool provider
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"""
|
|
|
|
|
if schema_type not in [member.value for member in ApiProviderSchemaType]:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
raise ValueError(f"invalid schema type {schema}")
|
|
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
|
# check if the provider exists
|
2024-08-26 13:43:57 +08:00
|
|
|
|
provider: ApiToolProvider = (
|
|
|
|
|
db.session.query(ApiToolProvider)
|
|
|
|
|
.filter(
|
|
|
|
|
ApiToolProvider.tenant_id == tenant_id,
|
|
|
|
|
ApiToolProvider.name == original_provider,
|
|
|
|
|
)
|
|
|
|
|
.first()
|
|
|
|
|
)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
if provider is None:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
raise ValueError(f"api provider {provider_name} does not exists")
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
# parse openapi to tool bundle
|
|
|
|
|
extra_info = {}
|
|
|
|
|
# extra info like description will be set here
|
2024-05-27 22:01:11 +08:00
|
|
|
|
tool_bundles, schema_type = ApiToolManageService.convert_schema_to_tool_bundles(schema, extra_info)
|
2024-08-26 13:43:57 +08:00
|
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
|
# update db provider
|
|
|
|
|
provider.name = provider_name
|
2024-02-01 09:10:32 +08:00
|
|
|
|
provider.icon = json.dumps(icon)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
provider.schema = schema
|
2024-08-26 13:43:57 +08:00
|
|
|
|
provider.description = extra_info.get("description", "")
|
2024-01-23 19:58:23 +08:00
|
|
|
|
provider.schema_type_str = ApiProviderSchemaType.OPENAPI.value
|
2024-04-08 18:51:46 +08:00
|
|
|
|
provider.tools_str = json.dumps(jsonable_encoder(tool_bundles))
|
2024-01-23 19:58:23 +08:00
|
|
|
|
provider.privacy_policy = privacy_policy
|
2024-05-18 10:52:48 +08:00
|
|
|
|
provider.custom_disclaimer = custom_disclaimer
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
2024-08-26 13:43:57 +08:00
|
|
|
|
if "auth_type" not in credentials:
|
|
|
|
|
raise ValueError("auth_type is required")
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
# get auth type, none or api key
|
2024-08-26 13:43:57 +08:00
|
|
|
|
auth_type = ApiProviderAuthType.value_of(credentials["auth_type"])
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
# create provider entity
|
2024-05-27 22:01:11 +08:00
|
|
|
|
provider_controller = ApiToolProviderController.from_db(provider, auth_type)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
# load tools into provider entity
|
2024-02-12 18:17:43 +08:00
|
|
|
|
provider_controller.load_bundled_tools(tool_bundles)
|
|
|
|
|
|
|
|
|
|
# get original credentials if exists
|
2024-03-08 20:31:13 +08:00
|
|
|
|
tool_configuration = ToolConfigurationManager(tenant_id=tenant_id, provider_controller=provider_controller)
|
2024-02-12 18:17:43 +08:00
|
|
|
|
|
|
|
|
|
original_credentials = tool_configuration.decrypt_tool_credentials(provider.credentials)
|
|
|
|
|
masked_credentials = tool_configuration.mask_tool_credentials(original_credentials)
|
|
|
|
|
# check if the credential has changed, save the original credential
|
|
|
|
|
for name, value in credentials.items():
|
|
|
|
|
if name in masked_credentials and value == masked_credentials[name]:
|
|
|
|
|
credentials[name] = original_credentials[name]
|
|
|
|
|
|
|
|
|
|
credentials = tool_configuration.encrypt_tool_credentials(credentials)
|
|
|
|
|
provider.credentials_str = json.dumps(credentials)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
db.session.add(provider)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
2024-02-12 18:17:43 +08:00
|
|
|
|
# delete cache
|
|
|
|
|
tool_configuration.delete_tool_credentials_cache()
|
|
|
|
|
|
2024-05-27 22:01:11 +08:00
|
|
|
|
# update labels
|
|
|
|
|
ToolLabelManager.update_tool_labels(provider_controller, labels)
|
2024-02-12 18:17:43 +08:00
|
|
|
|
|
2024-08-26 13:43:57 +08:00
|
|
|
|
return {"result": "success"}
|
|
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
|
@staticmethod
|
2024-08-26 13:43:57 +08:00
|
|
|
|
def delete_api_tool_provider(user_id: str, tenant_id: str, provider_name: str):
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
delete tool provider
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
provider: ApiToolProvider = (
|
|
|
|
|
db.session.query(ApiToolProvider)
|
|
|
|
|
.filter(
|
|
|
|
|
ApiToolProvider.tenant_id == tenant_id,
|
|
|
|
|
ApiToolProvider.name == provider_name,
|
|
|
|
|
)
|
|
|
|
|
.first()
|
|
|
|
|
)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
if provider is None:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
raise ValueError(f"you have not added provider {provider_name}")
|
|
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
|
db.session.delete(provider)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
2024-08-26 13:43:57 +08:00
|
|
|
|
return {"result": "success"}
|
|
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
|
@staticmethod
|
2024-08-26 13:43:57 +08:00
|
|
|
|
def get_api_tool_provider(user_id: str, tenant_id: str, provider: str):
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
get api tool provider
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"""
|
|
|
|
|
return ToolManager.user_get_api_provider(provider=provider, tenant_id=tenant_id)
|
2024-08-26 13:43:57 +08:00
|
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
|
@staticmethod
|
|
|
|
|
def test_api_tool_preview(
|
2024-08-26 13:43:57 +08:00
|
|
|
|
tenant_id: str,
|
2024-03-04 14:16:47 +08:00
|
|
|
|
provider_name: str,
|
2024-08-26 13:43:57 +08:00
|
|
|
|
tool_name: str,
|
|
|
|
|
credentials: dict,
|
|
|
|
|
parameters: dict,
|
|
|
|
|
schema_type: str,
|
|
|
|
|
schema: str,
|
2024-01-23 19:58:23 +08:00
|
|
|
|
):
|
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
test api tool before adding api tool provider
|
2024-01-23 19:58:23 +08:00
|
|
|
|
"""
|
|
|
|
|
if schema_type not in [member.value for member in ApiProviderSchemaType]:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
raise ValueError(f"invalid schema type {schema_type}")
|
|
|
|
|
|
2024-01-30 22:22:58 +08:00
|
|
|
|
try:
|
|
|
|
|
tool_bundles, _ = ApiBasedToolSchemaParser.auto_parse_to_tool_bundle(schema)
|
|
|
|
|
except Exception as e:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
raise ValueError("invalid schema")
|
|
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
|
# get tool bundle
|
|
|
|
|
tool_bundle = next(filter(lambda tb: tb.operation_id == tool_name, tool_bundles), None)
|
|
|
|
|
if tool_bundle is None:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
raise ValueError(f"invalid tool name {tool_name}")
|
|
|
|
|
|
|
|
|
|
db_provider: ApiToolProvider = (
|
|
|
|
|
db.session.query(ApiToolProvider)
|
|
|
|
|
.filter(
|
|
|
|
|
ApiToolProvider.tenant_id == tenant_id,
|
|
|
|
|
ApiToolProvider.name == provider_name,
|
|
|
|
|
)
|
|
|
|
|
.first()
|
|
|
|
|
)
|
2024-03-04 14:16:47 +08:00
|
|
|
|
|
|
|
|
|
if not db_provider:
|
|
|
|
|
# create a fake db provider
|
|
|
|
|
db_provider = ApiToolProvider(
|
2024-08-26 13:43:57 +08:00
|
|
|
|
tenant_id="",
|
|
|
|
|
user_id="",
|
|
|
|
|
name="",
|
|
|
|
|
icon="",
|
2024-03-04 14:16:47 +08:00
|
|
|
|
schema=schema,
|
2024-08-26 13:43:57 +08:00
|
|
|
|
description="",
|
2024-03-04 14:16:47 +08:00
|
|
|
|
schema_type_str=ApiProviderSchemaType.OPENAPI.value,
|
2024-04-08 18:51:46 +08:00
|
|
|
|
tools_str=json.dumps(jsonable_encoder(tool_bundles)),
|
2024-03-04 14:16:47 +08:00
|
|
|
|
credentials_str=json.dumps(credentials),
|
|
|
|
|
)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
2024-08-26 13:43:57 +08:00
|
|
|
|
if "auth_type" not in credentials:
|
|
|
|
|
raise ValueError("auth_type is required")
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
# get auth type, none or api key
|
2024-08-26 13:43:57 +08:00
|
|
|
|
auth_type = ApiProviderAuthType.value_of(credentials["auth_type"])
|
2024-01-23 19:58:23 +08:00
|
|
|
|
|
|
|
|
|
# create provider entity
|
2024-05-27 22:01:11 +08:00
|
|
|
|
provider_controller = ApiToolProviderController.from_db(db_provider, auth_type)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
# load tools into provider entity
|
|
|
|
|
provider_controller.load_bundled_tools(tool_bundles)
|
|
|
|
|
|
2024-03-04 14:16:47 +08:00
|
|
|
|
# decrypt credentials
|
|
|
|
|
if db_provider.id:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
tool_configuration = ToolConfigurationManager(tenant_id=tenant_id, provider_controller=provider_controller)
|
2024-03-04 14:16:47 +08:00
|
|
|
|
decrypted_credentials = tool_configuration.decrypt_tool_credentials(credentials)
|
|
|
|
|
# check if the credential has changed, save the original credential
|
|
|
|
|
masked_credentials = tool_configuration.mask_tool_credentials(decrypted_credentials)
|
|
|
|
|
for name, value in credentials.items():
|
|
|
|
|
if name in masked_credentials and value == masked_credentials[name]:
|
|
|
|
|
credentials[name] = decrypted_credentials[name]
|
|
|
|
|
|
2024-01-23 19:58:23 +08:00
|
|
|
|
try:
|
|
|
|
|
provider_controller.validate_credentials_format(credentials)
|
|
|
|
|
# get tool
|
|
|
|
|
tool = provider_controller.get_tool(tool_name)
|
2024-08-26 13:43:57 +08:00
|
|
|
|
tool = tool.fork_tool_runtime(
|
|
|
|
|
runtime={
|
|
|
|
|
"credentials": credentials,
|
|
|
|
|
"tenant_id": tenant_id,
|
|
|
|
|
}
|
|
|
|
|
)
|
2024-02-05 18:48:30 +08:00
|
|
|
|
result = tool.validate_credentials(credentials, parameters)
|
2024-01-23 19:58:23 +08:00
|
|
|
|
except Exception as e:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
return {"result": result or "empty response"}
|
|
|
|
|
|
2024-04-08 18:51:46 +08:00
|
|
|
|
@staticmethod
|
2024-08-26 13:43:57 +08:00
|
|
|
|
def list_api_tools(user_id: str, tenant_id: str) -> list[UserToolProvider]:
|
2024-04-08 18:51:46 +08:00
|
|
|
|
"""
|
2024-08-26 13:43:57 +08:00
|
|
|
|
list api tools
|
2024-04-08 18:51:46 +08:00
|
|
|
|
"""
|
|
|
|
|
# get all api providers
|
2024-08-26 13:43:57 +08:00
|
|
|
|
db_providers: list[ApiToolProvider] = (
|
|
|
|
|
db.session.query(ApiToolProvider).filter(ApiToolProvider.tenant_id == tenant_id).all() or []
|
|
|
|
|
)
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
|
|
result: list[UserToolProvider] = []
|
|
|
|
|
|
|
|
|
|
for provider in db_providers:
|
|
|
|
|
# convert provider controller to user provider
|
|
|
|
|
provider_controller = ToolTransformService.api_provider_to_controller(db_provider=provider)
|
2024-05-27 22:01:11 +08:00
|
|
|
|
labels = ToolLabelManager.get_tool_labels(provider_controller)
|
2024-04-08 18:51:46 +08:00
|
|
|
|
user_provider = ToolTransformService.api_provider_to_user_provider(
|
2024-08-26 13:43:57 +08:00
|
|
|
|
provider_controller, db_provider=provider, decrypt_credentials=True
|
2024-04-08 18:51:46 +08:00
|
|
|
|
)
|
2024-07-12 09:02:25 +08:00
|
|
|
|
user_provider.labels = labels
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
|
|
# add icon
|
|
|
|
|
ToolTransformService.repack_provider(user_provider)
|
|
|
|
|
|
2024-08-26 13:43:57 +08:00
|
|
|
|
tools = provider_controller.get_tools(user_id=user_id, tenant_id=tenant_id)
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
|
|
for tool in tools:
|
2024-08-26 13:43:57 +08:00
|
|
|
|
user_provider.tools.append(
|
|
|
|
|
ToolTransformService.tool_to_user_tool(
|
|
|
|
|
tenant_id=tenant_id, tool=tool, credentials=user_provider.original_credentials, labels=labels
|
|
|
|
|
)
|
|
|
|
|
)
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
|
|
|
|
result.append(user_provider)
|
|
|
|
|
|
|
|
|
|
return result
|