您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

string_transform.py 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import os
  17. import re
  18. from abc import ABC
  19. from jinja2 import Template as Jinja2Template
  20. from agent.component.base import ComponentParamBase
  21. from api.utils.api_utils import timeout
  22. from .message import Message
  23. class StringTransformParam(ComponentParamBase):
  24. """
  25. Define the code sandbox component parameters.
  26. """
  27. def __init__(self):
  28. super().__init__()
  29. self.method = "split"
  30. self.script = ""
  31. self.split_ref = ""
  32. self.delimiters = [","]
  33. self.outputs = {"result": {"value": "", "type": "string"}}
  34. def check(self):
  35. self.check_valid_value(self.method, "Support method", ["split", "merge"])
  36. self.check_empty(self.delimiters, "delimiters")
  37. class StringTransform(Message, ABC):
  38. component_name = "StringTransform"
  39. def get_input_form(self) -> dict[str, dict]:
  40. if self._param.method == "split":
  41. return {
  42. "line": {
  43. "name": "String",
  44. "type": "line"
  45. }
  46. }
  47. return {k: {
  48. "name": o["name"],
  49. "type": "line"
  50. } for k, o in self.get_input_elements_from_text(self._param.script).items()}
  51. @timeout(os.environ.get("COMPONENT_EXEC_TIMEOUT", 10*60))
  52. def _invoke(self, **kwargs):
  53. if self._param.method == "split":
  54. self._split(kwargs.get("line"))
  55. else:
  56. self._merge(kwargs)
  57. def _split(self, line:str|None = None):
  58. var = self._canvas.get_variable_value(self._param.split_ref) if not line else line
  59. if not var:
  60. var = ""
  61. assert isinstance(var, str), "The input variable is not a string: {}".format(type(var))
  62. self.set_input_value(self._param.split_ref, var)
  63. res = []
  64. for i,s in enumerate(re.split(r"(%s)"%("|".join([re.escape(d) for d in self._param.delimiters])), var, flags=re.DOTALL)):
  65. if i % 2 == 1:
  66. continue
  67. res.append(s)
  68. self.set_output("result", res)
  69. def _merge(self, kwargs:dict[str, str] = {}):
  70. script = self._param.script
  71. script, kwargs = self.get_kwargs(script, kwargs, self._param.delimiters[0])
  72. if self._is_jinjia2(script):
  73. template = Jinja2Template(script)
  74. try:
  75. script = template.render(kwargs)
  76. except Exception:
  77. pass
  78. for k,v in kwargs.items():
  79. if not v:
  80. v = ""
  81. script = re.sub(k, v, script)
  82. self.set_output("result", script)