flow.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from __future__ import annotations
  2. from typing import Any
  3. from ..context import WorkflowContext
  4. from ..registry import control_ports, register_node
  5. def start_node(node: dict[str, Any], inputs: dict[str, Any], context: WorkflowContext) -> dict[str, Any]:
  6. return {"message": "workflow started"}
  7. def end_node(node: dict[str, Any], inputs: dict[str, Any], context: WorkflowContext) -> dict[str, Any]:
  8. return {"message": "workflow ended"}
  9. def condition_node(node: dict[str, Any], inputs: dict[str, Any], context: WorkflowContext) -> dict[str, Any]:
  10. left = inputs.get("left", node.get("params", {}).get("left"))
  11. right = inputs.get("right", node.get("params", {}).get("right"))
  12. operator = node.get("params", {}).get("operator") or "equals"
  13. matched = {
  14. "equals": left == right,
  15. "not_equals": left != right,
  16. "truthy": bool(left),
  17. "falsy": not bool(left),
  18. "contains": str(right) in str(left),
  19. }.get(operator, False)
  20. return {"matched": matched, "next_port": "true" if matched else "false"}
  21. register_node(
  22. {
  23. "type": "flow.start",
  24. "category": "flow",
  25. "label": "开始",
  26. "params": {},
  27. "inputs": {},
  28. "outputs": {},
  29. "control_ports": {"inputs": [], "outputs": ["next"]},
  30. },
  31. start_node,
  32. )
  33. register_node(
  34. {
  35. "type": "flow.end",
  36. "category": "flow",
  37. "label": "结束",
  38. "params": {},
  39. "inputs": {},
  40. "outputs": {},
  41. "control_ports": {"inputs": ["run"], "outputs": []},
  42. },
  43. end_node,
  44. )
  45. register_node(
  46. {
  47. "type": "flow.condition",
  48. "category": "flow",
  49. "label": "条件判断",
  50. "params": {
  51. "operator": {
  52. "type": "select",
  53. "label": "判断方式",
  54. "default": "equals",
  55. "options": ["equals", "not_equals", "truthy", "falsy", "contains"],
  56. },
  57. "left": {"type": "text", "label": "左值"},
  58. "right": {"type": "text", "label": "右值"},
  59. },
  60. "inputs": {
  61. "left": {"type": "any", "label": "左值"},
  62. "right": {"type": "any", "label": "右值"},
  63. },
  64. "outputs": {"matched": {"type": "boolean", "label": "是否匹配"}},
  65. "control_ports": control_ports(["true", "false", "failure"]),
  66. },
  67. condition_node,
  68. )