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.

workflow_fields.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from flask_restful import fields
  2. from core.helper import encrypter
  3. from core.variables import SecretVariable, SegmentType, Variable
  4. from fields.member_fields import simple_account_fields
  5. from libs.helper import TimestampField
  6. from ._value_type_serializer import serialize_value_type
  7. ENVIRONMENT_VARIABLE_SUPPORTED_TYPES = (SegmentType.STRING, SegmentType.NUMBER, SegmentType.SECRET)
  8. class EnvironmentVariableField(fields.Raw):
  9. def format(self, value):
  10. # Mask secret variables values in environment_variables
  11. if isinstance(value, SecretVariable):
  12. return {
  13. "id": value.id,
  14. "name": value.name,
  15. "value": encrypter.obfuscated_token(value.value),
  16. "value_type": value.value_type.value,
  17. "description": value.description,
  18. }
  19. if isinstance(value, Variable):
  20. return {
  21. "id": value.id,
  22. "name": value.name,
  23. "value": value.value,
  24. "value_type": value.value_type.exposed_type().value,
  25. "description": value.description,
  26. }
  27. if isinstance(value, dict):
  28. value_type_str = value.get("value_type")
  29. if not isinstance(value_type_str, str):
  30. raise TypeError(
  31. f"unexpected type for value_type field, value={value_type_str}, type={type(value_type_str)}"
  32. )
  33. value_type = SegmentType(value_type_str).exposed_type()
  34. if value_type not in ENVIRONMENT_VARIABLE_SUPPORTED_TYPES:
  35. raise ValueError(f"Unsupported environment variable value type: {value_type}")
  36. return value
  37. conversation_variable_fields = {
  38. "id": fields.String,
  39. "name": fields.String,
  40. "value_type": fields.String(attribute=serialize_value_type),
  41. "value": fields.Raw,
  42. "description": fields.String,
  43. }
  44. workflow_fields = {
  45. "id": fields.String,
  46. "graph": fields.Raw(attribute="graph_dict"),
  47. "features": fields.Raw(attribute="features_dict"),
  48. "hash": fields.String(attribute="unique_hash"),
  49. "version": fields.String,
  50. "marked_name": fields.String,
  51. "marked_comment": fields.String,
  52. "created_by": fields.Nested(simple_account_fields, attribute="created_by_account"),
  53. "created_at": TimestampField,
  54. "updated_by": fields.Nested(simple_account_fields, attribute="updated_by_account", allow_null=True),
  55. "updated_at": TimestampField,
  56. "tool_published": fields.Boolean,
  57. "environment_variables": fields.List(EnvironmentVariableField()),
  58. "conversation_variables": fields.List(fields.Nested(conversation_variable_fields)),
  59. }
  60. workflow_partial_fields = {
  61. "id": fields.String,
  62. "created_by": fields.String,
  63. "created_at": TimestampField,
  64. "updated_by": fields.String,
  65. "updated_at": TimestampField,
  66. }
  67. workflow_pagination_fields = {
  68. "items": fields.List(fields.Nested(workflow_fields), attribute="items"),
  69. "page": fields.Integer,
  70. "limit": fields.Integer(attribute="limit"),
  71. "has_more": fields.Boolean(attribute="has_more"),
  72. }