Skip to content

Commit 84b7f68

Browse files
committed
feat: add support for SKILL tool type with file encoding and saving functionality
1 parent 40edcde commit 84b7f68

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

apps/tools/serializers/tool.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import asyncio
3+
import base64
34
import io
45
import json
56
import os
@@ -613,6 +614,11 @@ def export(self):
613614
id = self.data.get('id')
614615
tool = QuerySet(Tool).filter(id=id).first()
615616
tool_dict = ToolExportModelSerializer(tool).data
617+
# 如果是SKILL类型的工具,校验文件是否存在
618+
if tool.tool_type == ToolType.SKILL:
619+
skill_file = QuerySet(File).filter(id=tool.code).first()
620+
if skill_file:
621+
tool_dict['code'] = base64.b64encode(skill_file.get_bytes()).decode('utf-8')
616622
mk_instance = ToolInstance(tool_dict, 'v2')
617623
tool_pickle = pickle.dumps(mk_instance)
618624
response = HttpResponse(content_type='text/plain', content=tool_pickle)
@@ -662,11 +668,23 @@ def import_(self, scope=ToolScope.WORKSPACE):
662668
folder_id = self.data.get('folder_id')
663669
tool = tool_instance.tool
664670
tool_id = uuid.uuid7()
671+
code = tool.get('code')
672+
if tool.get('tool_type') == ToolType.SKILL:
673+
skill_file_id = uuid.uuid7()
674+
skill_file = File(
675+
id=skill_file_id,
676+
file_name=f"{tool.get('name')}.zip",
677+
source_type=FileSourceType.TOOL,
678+
source_id=tool_id,
679+
meta={}
680+
)
681+
skill_file.save(base64.b64decode(code))
682+
code = skill_file_id
665683
tool_model = Tool(
666684
id=tool_id,
667685
name=tool.get('name'),
668686
desc=tool.get('desc'),
669-
code=tool.get('code'),
687+
code=code,
670688
user_id=user_id,
671689
workspace_id=self.data.get('workspace_id'),
672690
input_field_list=tool.get('input_field_list'),

0 commit comments

Comments
 (0)