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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. ENVIRONMENT_VARIABLE_SUPPORTED_TYPES = (SegmentType.STRING, SegmentType.NUMBER, SegmentType.SECRET)
  7. class EnvironmentVariableField(fields.Raw):
  8. def format(self, value):
  9. # Mask secret variables values in environment_variables
  10. if isinstance(value, SecretVariable):
  11. return {
  12. "id": value.id,
  13. "name": value.name,
  14. "value": encrypter.obfuscated_token(value.value),
  15. "value_type": value.value_type.value,
  16. "description": value.description,
  17. }
  18. if isinstance(value, Variable):
  19. return {
  20. "id": value.id,
  21. "name": value.name,
  22. "value": value.value,
  23. "value_type": value.value_type.value,
  24. "description": value.description,
  25. }
  26. if isinstance(value, dict):
  27. value_type = value.get("value_type")
  28. if value_type not in ENVIRONMENT_VARIABLE_SUPPORTED_TYPES:
  29. raise ValueError(f"Unsupported environment variable value type: {value_type}")
  30. return value
  31. conversation_variable_fields = {
  32. "id": fields.String,
  33. "name": fields.String,
  34. "value_type": fields.String(attribute="value_type.value"),
  35. "value": fields.Raw,
  36. "description": fields.String,
  37. }
  38. workflow_fields = {
  39. "id": fields.String,
  40. "graph": fields.Raw(attribute="graph_dict"),
  41. "features": fields.Raw(attribute="features_dict"),
  42. "hash": fields.String(attribute="unique_hash"),
  43. "version": fields.String,
  44. "marked_name": fields.String,
  45. "marked_comment": fields.String,
  46. "created_by": fields.Nested(simple_account_fields, attribute="created_by_account"),
  47. "created_at": TimestampField,
  48. "updated_by": fields.Nested(simple_account_fields, attribute="updated_by_account", allow_null=True),
  49. "updated_at": TimestampField,
  50. "tool_published": fields.Boolean,
  51. "environment_variables": fields.List(EnvironmentVariableField()),
  52. "conversation_variables": fields.List(fields.Nested(conversation_variable_fields)),
  53. }
  54. workflow_partial_fields = {
  55. "id": fields.String,
  56. "created_by": fields.String,
  57. "created_at": TimestampField,
  58. "updated_by": fields.String,
  59. "updated_at": TimestampField,
  60. }
  61. workflow_pagination_fields = {
  62. "items": fields.List(fields.Nested(workflow_fields), attribute="items"),
  63. "page": fields.Integer,
  64. "limit": fields.Integer(attribute="limit"),
  65. "has_more": fields.Boolean(attribute="has_more"),
  66. }