| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  | from typing import Literal, Optional | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | from pydantic import BaseModel | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  | from core.model_runtime.utils.encoders import jsonable_encoder | 
					
						
							| 
									
										
										
										
											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_entities import ToolProviderCredentials, ToolProviderType | 
					
						
							| 
									
										
										
										
											2024-01-31 11:58:07 +08:00
										 |  |  | from core.tools.tool.tool import ToolParameter | 
					
						
							| 
									
										
										
										
											2024-02-01 18:11:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | class UserTool(BaseModel): | 
					
						
							|  |  |  |     author: str | 
					
						
							|  |  |  |     name: str # identifier | 
					
						
							|  |  |  |     label: I18nObject # label | 
					
						
							|  |  |  |     description: I18nObject | 
					
						
							| 
									
										
										
										
											2024-06-14 01:05:37 +08:00
										 |  |  |     parameters: Optional[list[ToolParameter]] = None | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  |     labels: list[str] = None | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  | UserToolProviderTypeLiteral = Optional[Literal[ | 
					
						
							|  |  |  |     'builtin', 'api', 'workflow' | 
					
						
							|  |  |  | ]] | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  | class UserToolProvider(BaseModel): | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     id: str | 
					
						
							|  |  |  |     author: str | 
					
						
							|  |  |  |     name: str # identifier | 
					
						
							|  |  |  |     description: I18nObject | 
					
						
							|  |  |  |     icon: str | 
					
						
							|  |  |  |     label: I18nObject # label | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  |     type: ToolProviderType | 
					
						
							| 
									
										
										
										
											2024-06-14 02:28:28 +08:00
										 |  |  |     masked_credentials: Optional[dict] = None | 
					
						
							|  |  |  |     original_credentials: Optional[dict] = None | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     is_team_authorization: bool = False | 
					
						
							|  |  |  |     allow_delete: bool = True | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     tools: list[UserTool] = None | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  |     labels: list[str] = None | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def to_dict(self) -> dict: | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  |         # ------------- | 
					
						
							|  |  |  |         # overwrite tool parameter types for temp fix | 
					
						
							|  |  |  |         tools = jsonable_encoder(self.tools) | 
					
						
							|  |  |  |         for tool in tools: | 
					
						
							|  |  |  |             if tool.get('parameters'): | 
					
						
							|  |  |  |                 for parameter in tool.get('parameters'): | 
					
						
							|  |  |  |                     if parameter.get('type') == ToolParameter.ToolParameterType.FILE.value: | 
					
						
							|  |  |  |                         parameter['type'] = 'files' | 
					
						
							|  |  |  |         # ------------- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         return { | 
					
						
							|  |  |  |             'id': self.id, | 
					
						
							|  |  |  |             'author': self.author, | 
					
						
							|  |  |  |             'name': self.name, | 
					
						
							|  |  |  |             'description': self.description.to_dict(), | 
					
						
							|  |  |  |             'icon': self.icon, | 
					
						
							|  |  |  |             'label': self.label.to_dict(), | 
					
						
							|  |  |  |             'type': self.type.value, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             'team_credentials': self.masked_credentials, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |             'is_team_authorization': self.is_team_authorization, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |             'allow_delete': self.allow_delete, | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  |             'tools': tools, | 
					
						
							|  |  |  |             'labels': self.labels, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class UserToolProviderCredentials(BaseModel): | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     credentials: dict[str, ToolProviderCredentials] |