feat: tool credentials cache and introduce _position.yaml (#2386)

This commit is contained in:
Yeuoly
2024-02-05 12:39:42 +08:00
committed by GitHub
parent 6278ff0f30
commit 5010706d8b
4 changed files with 96 additions and 26 deletions

View File

@@ -1,10 +1,10 @@
from typing import Any, Dict
from core.helper import encrypter
from core.tools.entities.tool_entities import ToolProviderCredentials
from core.tools.provider.tool_provider import ToolProviderController
from typing import Dict, Any
from pydantic import BaseModel
from core.tools.entities.tool_entities import ToolProviderCredentials
from core.tools.provider.tool_provider import ToolProviderController
from core.helper import encrypter
from core.helper.tool_provider_cache import ToolProviderCredentialsCacheType, ToolProviderCredentialsCache
class ToolConfiguration(BaseModel):
tenant_id: str
@@ -63,8 +63,15 @@ class ToolConfiguration(BaseModel):
return a deep copy of credentials with decrypted values
"""
cache = ToolProviderCredentialsCache(
tenant_id=self.tenant_id,
identity_id=f'{self.provider_controller.app_type.value}.{self.provider_controller.identity.name}',
cache_type=ToolProviderCredentialsCacheType.PROVIDER
)
cached_credentials = cache.get()
if cached_credentials:
return cached_credentials
credentials = self._deep_copy(credentials)
# get fields need to be decrypted
fields = self.provider_controller.get_credentials_schema()
for field_name, field in fields.items():
@@ -74,5 +81,6 @@ class ToolConfiguration(BaseModel):
credentials[field_name] = encrypter.decrypt_token(self.tenant_id, credentials[field_name])
except:
pass
cache.set(credentials)
return credentials