Skip to content

Commit 1a8fd14

Browse files
committed
feat: add support for encoding skill tool files in base64 format
1 parent 308b370 commit 1a8fd14

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

apps/tools/serializers/tool.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ def hand_node(node, update_tool_map):
7171
tool_ids = node_data.get('tool_ids') or []
7272
node_data['tool_ids'] = [update_tool_map.get(tool_id,
7373
tool_id) for tool_id in tool_ids]
74+
skill_tool_ids = node_data.get('skill_tool_ids') or []
75+
node_data['skill_tool_ids'] = [update_tool_map.get(tool_id,
76+
tool_id) for tool_id in skill_tool_ids]
7477
if node.get('type') == 'mcp-node':
7578
mcp_tool_id = (node.get('properties', {}).get('node_data', {}).get('mcp_tool_id') or '')
7679
node.get('properties', {}).get('node_data', {})['mcp_tool_id'] = update_tool_map.get(mcp_tool_id,
@@ -695,6 +698,12 @@ def get_child_tool_list(self, work_flow, response):
695698
self.get_child_tool_list(work_flow_tool_dict.get(tool.id).work_flow, response)
696699
else:
697700
response.append(ToolExportModelSerializer(tool).data)
701+
skill_tools = [tool for tool in tool_list if tool.tool_type == ToolType.SKILL]
702+
for tool in skill_tools:
703+
skill_file = QuerySet(File).filter(id=tool.code).first()
704+
if skill_file:
705+
tool.code = base64.b64encode(skill_file.get_bytes()).decode('utf-8')
706+
response.append(ToolExportModelSerializer(tool).data)
698707
else:
699708
for tool in tool_list:
700709
response.append(ToolExportModelSerializer(tool).data)
@@ -759,6 +768,19 @@ def to_tool_workflow(work_flow, update_tool_map):
759768

760769
@staticmethod
761770
def to_tool(tool, workspace_id, user_id, folder_id):
771+
# 如果是技能类型的工具,需要将code保存为文件
772+
code = tool.get('code')
773+
if tool.get('tool_type') == ToolType.SKILL:
774+
skill_file_id = uuid.uuid7()
775+
skill_file = File(
776+
id=skill_file_id,
777+
file_name=f"{tool.get('name')}.zip",
778+
source_type=FileSourceType.TOOL,
779+
source_id=tool.get('id'),
780+
meta={}
781+
)
782+
skill_file.save(base64.b64decode(code))
783+
tool['code'] = skill_file_id
762784
return Tool(id=tool.get('id'),
763785
user_id=user_id,
764786
name=tool.get('name'),
@@ -831,7 +853,7 @@ def import_workflow_tools(self, tool, workspace_id, user_id, folder_id, new_chil
831853
workflow_tool_model_list = [{'tool_id': t.get('id'), 'workflow': self.to_tool_workflow(
832854
t.get('work_flow'),
833855
update_tool_map,
834-
)} for t in tool_list if tool.get('tool_type') == ToolType.WORKFLOW]
856+
)} for t in tool_list if t.get('tool_type') == ToolType.WORKFLOW]
835857

836858
existing_records = QuerySet(ToolWorkflow).filter(
837859
tool_id__in=[wt.get('tool_id') for wt in workflow_tool_model_list],

0 commit comments

Comments
 (0)