(self, scope=ToolScope.WORKSPACE, name=None, source=None)
| 1079 | |
| 1080 | @transaction.atomic |
| 1081 | def import_(self, scope=ToolScope.WORKSPACE, name=None, source=None): |
| 1082 | self.is_valid() |
| 1083 | |
| 1084 | user_id = self.data.get("user_id") |
| 1085 | tool_instance_bytes = self.data.get("file").read() |
| 1086 | try: |
| 1087 | tool_instance = RestrictedUnpickler(io.BytesIO(tool_instance_bytes)).load() |
| 1088 | except Exception as e: |
| 1089 | raise AppApiException(1001, _("Unsupported file format")) |
| 1090 | if self.data.get("folder_id") is None: |
| 1091 | folder_id = self.data.get("workspace_id") |
| 1092 | else: |
| 1093 | folder_id = self.data.get("folder_id") |
| 1094 | tool = tool_instance.tool |
| 1095 | tool_id = uuid.uuid7() |
| 1096 | code = tool.get("code") |
| 1097 | if tool.get("tool_type") == ToolType.SKILL: |
| 1098 | skill_file_id = uuid.uuid7() |
| 1099 | skill_file = File( |
| 1100 | id=skill_file_id, |
| 1101 | file_name=f"{tool.get('name')}.zip", |
| 1102 | source_type=FileSourceType.TOOL, |
| 1103 | source_id=tool_id, |
| 1104 | meta={}, |
| 1105 | ) |
| 1106 | skill_file.save(base64.b64decode(code)) |
| 1107 | code = skill_file_id |
| 1108 | tool_model = Tool( |
| 1109 | id=tool_id, |
| 1110 | name=name or tool.get("name"), |
| 1111 | desc=tool.get("desc"), |
| 1112 | code=code, |
| 1113 | user_id=user_id, |
| 1114 | workspace_id=self.data.get("workspace_id"), |
| 1115 | input_field_list=tool.get("input_field_list"), |
| 1116 | init_field_list=tool.get("init_field_list", []), |
| 1117 | tool_type=tool.get("tool_type"), |
| 1118 | folder_id=folder_id, |
| 1119 | scope=scope, |
| 1120 | is_active=False, |
| 1121 | ) |
| 1122 | # 校验代码是否包括禁止的关键字 |
| 1123 | if tool.get("tool_type") == ToolType.MCP: |
| 1124 | ToolExecutor().validate_mcp_transport(code) |
| 1125 | tool_model.save() |
| 1126 | if tool.get("tool_type") == ToolType.WORKFLOW: |
| 1127 | tool["id"] = tool_id |
| 1128 | self.import_workflow_tools( |
| 1129 | tool, |
| 1130 | workspace_id=self.data.get("workspace_id"), |
| 1131 | user_id=user_id, |
| 1132 | folder_id=folder_id, |
| 1133 | new_child_policy=2 if source == "template" else 1, |
| 1134 | ) |
| 1135 | # 自动授权给创建者 |
| 1136 | UserResourcePermissionSerializer( |
| 1137 | data={ |
| 1138 | "workspace_id": self.data.get("workspace_id"), |
no test coverage detected