2024-04-08 18:51:46 +08:00
|
|
|
from flask_restful import fields
|
|
|
|
|
2024-07-25 11:30:52 +08:00
|
|
|
from core.app.segments import SecretVariable, SegmentType, Variable
|
2024-07-22 15:29:39 +08:00
|
|
|
from core.helper import encrypter
|
2024-04-08 18:51:46 +08:00
|
|
|
from fields.member_fields import simple_account_fields
|
|
|
|
from libs.helper import TimestampField
|
|
|
|
|
2024-07-25 11:30:52 +08:00
|
|
|
ENVIRONMENT_VARIABLE_SUPPORTED_TYPES = (SegmentType.STRING, SegmentType.NUMBER, SegmentType.SECRET)
|
|
|
|
|
2024-07-22 15:29:39 +08:00
|
|
|
|
|
|
|
class EnvironmentVariableField(fields.Raw):
|
|
|
|
def format(self, value):
|
|
|
|
# Mask secret variables values in environment_variables
|
|
|
|
if isinstance(value, SecretVariable):
|
|
|
|
return {
|
2024-08-15 12:54:05 +08:00
|
|
|
"id": value.id,
|
|
|
|
"name": value.name,
|
|
|
|
"value": encrypter.obfuscated_token(value.value),
|
|
|
|
"value_type": value.value_type.value,
|
2024-07-22 15:29:39 +08:00
|
|
|
}
|
2024-07-25 11:30:52 +08:00
|
|
|
if isinstance(value, Variable):
|
2024-07-22 15:29:39 +08:00
|
|
|
return {
|
2024-08-15 12:54:05 +08:00
|
|
|
"id": value.id,
|
|
|
|
"name": value.name,
|
|
|
|
"value": value.value,
|
|
|
|
"value_type": value.value_type.value,
|
2024-07-22 15:29:39 +08:00
|
|
|
}
|
2024-07-25 11:30:52 +08:00
|
|
|
if isinstance(value, dict):
|
2024-08-15 12:54:05 +08:00
|
|
|
value_type = value.get("value_type")
|
2024-07-25 11:30:52 +08:00
|
|
|
if value_type not in ENVIRONMENT_VARIABLE_SUPPORTED_TYPES:
|
2024-08-15 12:54:05 +08:00
|
|
|
raise ValueError(f"Unsupported environment variable value type: {value_type}")
|
2024-07-25 11:30:52 +08:00
|
|
|
return value
|
2024-07-22 15:29:39 +08:00
|
|
|
|
|
|
|
|
2024-08-13 14:44:10 +08:00
|
|
|
conversation_variable_fields = {
|
2024-08-15 12:54:05 +08:00
|
|
|
"id": fields.String,
|
|
|
|
"name": fields.String,
|
|
|
|
"value_type": fields.String(attribute="value_type.value"),
|
|
|
|
"value": fields.Raw,
|
|
|
|
"description": fields.String,
|
2024-07-22 15:29:39 +08:00
|
|
|
}
|
|
|
|
|
2024-04-08 18:51:46 +08:00
|
|
|
workflow_fields = {
|
2024-08-15 12:54:05 +08:00
|
|
|
"id": fields.String,
|
|
|
|
"graph": fields.Raw(attribute="graph_dict"),
|
|
|
|
"features": fields.Raw(attribute="features_dict"),
|
|
|
|
"hash": fields.String(attribute="unique_hash"),
|
|
|
|
"created_by": fields.Nested(simple_account_fields, attribute="created_by_account"),
|
|
|
|
"created_at": TimestampField,
|
|
|
|
"updated_by": fields.Nested(simple_account_fields, attribute="updated_by_account", allow_null=True),
|
|
|
|
"updated_at": TimestampField,
|
|
|
|
"tool_published": fields.Boolean,
|
|
|
|
"environment_variables": fields.List(EnvironmentVariableField()),
|
|
|
|
"conversation_variables": fields.List(fields.Nested(conversation_variable_fields)),
|
2024-04-08 18:51:46 +08:00
|
|
|
}
|