(self, instance, with_valid=True)
| 639 | |
| 640 | @transaction.atomic |
| 641 | def edit(self, instance, with_valid=True): |
| 642 | if with_valid: |
| 643 | self.is_valid(raise_exception=True) |
| 644 | ToolEditRequest(data=instance).is_valid(raise_exception=True) |
| 645 | # 校验代码是否包括禁止的关键字 |
| 646 | if instance.get("tool_type") == ToolType.MCP: |
| 647 | ToolExecutor().validate_mcp_transport(instance.get("code", "")) |
| 648 | |
| 649 | if not QuerySet(Tool).filter(id=self.data.get("id")).exists(): |
| 650 | raise serializers.ValidationError(_("Tool not found")) |
| 651 | |
| 652 | edit_field_list = [ |
| 653 | "name", |
| 654 | "desc", |
| 655 | "code", |
| 656 | "icon", |
| 657 | "input_field_list", |
| 658 | "init_field_list", |
| 659 | "init_params", |
| 660 | "is_active", |
| 661 | "folder_id", |
| 662 | ] |
| 663 | edit_dict = { |
| 664 | field: instance.get(field) |
| 665 | for field in edit_field_list |
| 666 | if (field in instance and instance.get(field) is not None) |
| 667 | } |
| 668 | |
| 669 | tool = QuerySet(Tool).filter(id=self.data.get("id")).first() |
| 670 | if "init_params" in edit_dict: |
| 671 | if edit_dict["init_field_list"] is not None: |
| 672 | rm_key = [] |
| 673 | for key in edit_dict["init_params"]: |
| 674 | if key not in [field["field"] for field in edit_dict["init_field_list"]]: |
| 675 | rm_key.append(key) |
| 676 | for key in rm_key: |
| 677 | edit_dict["init_params"].pop(key) |
| 678 | if tool.init_params: |
| 679 | old_init_params = json.loads(rsa_long_decrypt(tool.init_params)) |
| 680 | for key in edit_dict["init_params"]: |
| 681 | if key in old_init_params and edit_dict["init_params"][key] == encryption(old_init_params[key]): |
| 682 | edit_dict["init_params"][key] = old_init_params[key] |
| 683 | edit_dict["init_params"] = rsa_long_encrypt(json.dumps(edit_dict["init_params"])) |
| 684 | |
| 685 | edit_dict["update_time"] = timezone.now() |
| 686 | QuerySet(Tool).filter(id=self.data.get("id")).update(**edit_dict) |
| 687 | if "is_active" in instance: |
| 688 | QuerySet(TriggerTask).filter(source_type="TOOL", source_id=self.data.get("id")).update( |
| 689 | is_active=instance.get("is_active") |
| 690 | ) |
| 691 | |
| 692 | # 如果是SKILL类型的工具,修改file表中对应的记录 |
| 693 | if instance.get("tool_type") == ToolType.SKILL: |
| 694 | old_file_id = tool.code |
| 695 | file_id = instance.get("code") |
| 696 | if old_file_id != file_id: |
| 697 | QuerySet(File).filter(id=old_file_id).delete() |
| 698 | QuerySet(File).filter(id=file_id).update(source_id=tool.id, source_type=FileSourceType.TOOL) |
no test coverage detected