You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

iterationitem.py 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #
  2. # Copyright 2024 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. from abc import ABC
  17. from agent.component.base import ComponentBase, ComponentParamBase
  18. class IterationItemParam(ComponentParamBase):
  19. """
  20. Define the IterationItem component parameters.
  21. """
  22. def check(self):
  23. return True
  24. class IterationItem(ComponentBase, ABC):
  25. component_name = "IterationItem"
  26. def __init__(self, canvas, id, param: ComponentParamBase):
  27. super().__init__(canvas, id, param)
  28. self._idx = 0
  29. def _invoke(self, **kwargs):
  30. parent = self.get_parent()
  31. arr = self._canvas.get_variable_value(parent._param.items_ref)
  32. if not isinstance(arr, list):
  33. self._idx = -1
  34. raise Exception(parent._param.items_ref + " must be an array, but its type is "+str(type(arr)))
  35. if self._idx > 0:
  36. self.output_collation()
  37. if self._idx >= len(arr):
  38. self._idx = -1
  39. return
  40. self.set_output("item", arr[self._idx])
  41. self.set_output("index", self._idx)
  42. self._idx += 1
  43. def output_collation(self):
  44. pid = self.get_parent()._id
  45. for cid in self._canvas.components.keys():
  46. obj = self._canvas.get_component_obj(cid)
  47. p = obj.get_parent()
  48. if not p:
  49. continue
  50. if p._id != pid:
  51. continue
  52. if p.component_name.lower() in ["categorize", "message", "switch", "userfillup", "interationitem"]:
  53. continue
  54. for k, o in p._param.outputs.items():
  55. if "ref" not in o:
  56. continue
  57. _cid, var = o["ref"].split("@")
  58. if _cid != cid:
  59. continue
  60. res = p.output(k)
  61. if not res:
  62. res = []
  63. res.append(obj.output(var))
  64. p.set_output(k, res)
  65. def end(self):
  66. return self._idx == -1
  67. def thoughts(self) -> str:
  68. return "Next turn..."