| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |     name: str  # identifier | 
					
						
							|  |  |  |     label: I18nObject  # label | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     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-09-10 17:00:20 +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 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |     name: str  # identifier | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |     description: I18nObject | 
					
						
							|  |  |  |     icon: str | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |     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-10-21 10:43:49 +08:00
										 |  |  |     tools: list[UserTool] | None = None | 
					
						
							|  |  |  |     labels: list[str] | None = 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: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |             if tool.get("parameters"): | 
					
						
							|  |  |  |                 for parameter in tool.get("parameters"): | 
					
						
							| 
									
										
										
										
											2024-10-21 10:43:49 +08:00
										 |  |  |                     if parameter.get("type") == ToolParameter.ToolParameterType.SYSTEM_FILES.value: | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |                         parameter["type"] = "files" | 
					
						
							| 
									
										
										
										
											2024-05-27 22:01:11 +08:00
										 |  |  |         # ------------- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         return { | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |             "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, | 
					
						
							|  |  |  |             "team_credentials": self.masked_credentials, | 
					
						
							|  |  |  |             "is_team_authorization": self.is_team_authorization, | 
					
						
							|  |  |  |             "allow_delete": self.allow_delete, | 
					
						
							|  |  |  |             "tools": tools, | 
					
						
							|  |  |  |             "labels": self.labels, | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 19:58:23 +08:00
										 |  |  | class UserToolProviderCredentials(BaseModel): | 
					
						
							| 
									
										
										
										
											2024-09-10 17:00:20 +08:00
										 |  |  |     credentials: dict[str, ToolProviderCredentials] |