|  | @@ -1,11 +1,12 @@
 | 
											
												
													
														|  | -from base64 import b64decode
 |  | 
 | 
											
												
													
														|  |  from typing import Any, Union
 |  |  from typing import Any, Union
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  from httpx import post
 |  |  from httpx import post
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +from core.file.enums import FileType
 | 
											
												
													
														|  | 
 |  | +from core.file.file_manager import download
 | 
											
												
													
														|  | 
 |  | +from core.tools.entities.common_entities import I18nObject
 | 
											
												
													
														|  |  from core.tools.entities.tool_entities import ToolInvokeMessage, ToolParameter
 |  |  from core.tools.entities.tool_entities import ToolInvokeMessage, ToolParameter
 | 
											
												
													
														|  | -from core.tools.errors import ToolProviderCredentialValidationError
 |  | +from core.tools.errors import ToolParameterValidationError
 | 
											
												
													
														|  | -from core.tools.provider.builtin.vectorizer.tools.test_data import VECTORIZER_ICON_PNG
 |  | 
 | 
											
												
											
												
													
														|  |  from core.tools.tool.builtin_tool import BuiltinTool
 |  |  from core.tools.tool.builtin_tool import BuiltinTool
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  
 |  |  
 | 
											
										
											
												
													
														|  | @@ -16,30 +17,30 @@ class VectorizerTool(BuiltinTool):
 | 
											
												
													
														|  |          """
 |  |          """
 | 
											
												
													
														|  |          invoke tools
 |  |          invoke tools
 | 
											
												
													
														|  |          """
 |  |          """
 | 
											
												
													
														|  | -        api_key_name = self.runtime.credentials.get("api_key_name", None)
 |  | +        api_key_name = self.runtime.credentials.get("api_key_name")
 | 
											
												
													
														|  | -        api_key_value = self.runtime.credentials.get("api_key_value", None)
 |  | +        api_key_value = self.runtime.credentials.get("api_key_value")
 | 
											
												
											
												
											
												
													
														|  |          mode = tool_parameters.get("mode", "test")
 |  |          mode = tool_parameters.get("mode", "test")
 | 
											
												
													
														|  | -        if mode == "production":
 |  | 
 | 
											
												
													
														|  | -            mode = "preview"
 |  | 
 | 
											
												
													
														|  | -
 |  | 
 | 
											
												
													
														|  | -        if not api_key_name or not api_key_value:
 |  | 
 | 
											
												
													
														|  | -            raise ToolProviderCredentialValidationError("Please input api key name and value")
 |  | 
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +        
 | 
											
												
													
														|  | 
 |  | +        image = tool_parameters.get("image")
 | 
											
												
													
														|  | 
 |  | +        if image and image.type != FileType.IMAGE:
 | 
											
												
													
														|  | 
 |  | +            raise ToolParameterValidationError("Not a valid image")
 | 
											
												
													
														|  | 
 |  | +        
 | 
											
												
													
														|  |          image_id = tool_parameters.get("image_id", "")
 |  |          image_id = tool_parameters.get("image_id", "")
 | 
											
												
													
														|  | -        if not image_id:
 |  | 
 | 
											
												
													
														|  | -            return self.create_text_message("Please input image id")
 |  | 
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -        if image_id.startswith("__test_"):
 |  | +        if image_id:
 | 
											
												
													
														|  | -            image_binary = b64decode(VECTORIZER_ICON_PNG)
 |  | 
 | 
											
												
													
														|  | -        else:
 |  | 
 | 
											
												
											
												
													
														|  |              image_binary = self.get_variable_file(self.VariableKey.IMAGE)
 |  |              image_binary = self.get_variable_file(self.VariableKey.IMAGE)
 | 
											
												
													
														|  |              if not image_binary:
 |  |              if not image_binary:
 | 
											
												
													
														|  |                  return self.create_text_message("Image not found, please request user to generate image firstly.")
 |  |                  return self.create_text_message("Image not found, please request user to generate image firstly.")
 | 
											
												
													
														|  | 
 |  | +        elif image:
 | 
											
												
													
														|  | 
 |  | +            image_binary = download(image)
 | 
											
												
													
														|  | 
 |  | +        else:
 | 
											
												
													
														|  | 
 |  | +            raise ToolParameterValidationError("Please provide either image or image_id")
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |          response = post(
 |  |          response = post(
 | 
											
												
													
														|  |              "https://vectorizer.ai/api/v1/vectorize",
 |  |              "https://vectorizer.ai/api/v1/vectorize",
 | 
											
												
													
														|  | 
 |  | +            data={"mode": mode},
 | 
											
												
													
														|  |              files={"image": image_binary},
 |  |              files={"image": image_binary},
 | 
											
												
													
														|  | -            data={"mode": mode} if mode == "test" else {},
 |  | 
 | 
											
												
													
														|  |              auth=(api_key_name, api_key_value),
 |  |              auth=(api_key_name, api_key_value),
 | 
											
												
													
														|  |              timeout=30,
 |  |              timeout=30,
 | 
											
												
													
														|  |          )
 |  |          )
 | 
											
										
											
												
													
														|  | @@ -59,11 +60,23 @@ class VectorizerTool(BuiltinTool):
 | 
											
												
													
														|  |          return [
 |  |          return [
 | 
											
												
													
														|  |              ToolParameter.get_simple_instance(
 |  |              ToolParameter.get_simple_instance(
 | 
											
												
													
														|  |                  name="image_id",
 |  |                  name="image_id",
 | 
											
												
													
														|  | -                llm_description=f"the image id that you want to vectorize, \
 |  | +                llm_description=f"the image_id that you want to vectorize, \
 | 
											
												
													
														|  | -                    and the image id should be specified in \
 |  | +                    and the image_id should be specified in \
 | 
											
												
											
												
											
												
													
														|  |                          {[i.name for i in self.list_default_image_variables()]}",
 |  |                          {[i.name for i in self.list_default_image_variables()]}",
 | 
											
												
													
														|  |                  type=ToolParameter.ToolParameterType.SELECT,
 |  |                  type=ToolParameter.ToolParameterType.SELECT,
 | 
											
												
													
														|  | -                required=True,
 |  | +                required=False,
 | 
											
												
											
												
													
														|  |                  options=[i.name for i in self.list_default_image_variables()],
 |  |                  options=[i.name for i in self.list_default_image_variables()],
 | 
											
												
													
														|  | -            )
 |  | +            ),
 | 
											
												
											
												
													
														|  | 
 |  | +            ToolParameter(
 | 
											
												
													
														|  | 
 |  | +                name="image",
 | 
											
												
													
														|  | 
 |  | +                label=I18nObject(en_US="image", zh_Hans="image"),
 | 
											
												
													
														|  | 
 |  | +                human_description=I18nObject(
 | 
											
												
													
														|  | 
 |  | +                    en_US="The image to be converted.",
 | 
											
												
													
														|  | 
 |  | +                    zh_Hans="要转换的图片。",
 | 
											
												
													
														|  | 
 |  | +                ),
 | 
											
												
													
														|  | 
 |  | +                type=ToolParameter.ToolParameterType.FILE,
 | 
											
												
													
														|  | 
 |  | +                form=ToolParameter.ToolParameterForm.LLM,
 | 
											
												
													
														|  | 
 |  | +                llm_description="you should not input this parameter. just input the image_id.",
 | 
											
												
													
														|  | 
 |  | +                required=False,
 | 
											
												
													
														|  | 
 |  | +            ),
 | 
											
												
													
														|  |          ]
 |  |          ]
 |