2026-04-15 21:39:30 -07:00
|
|
|
from __future__ import annotations
|
2024-08-13 14:44:10 +08:00
|
|
|
|
2026-04-15 21:39:30 -07:00
|
|
|
from datetime import datetime
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
2026-06-02 15:27:23 +08:00
|
|
|
from flask_restx import fields
|
2026-04-15 21:39:30 -07:00
|
|
|
from pydantic import field_validator
|
|
|
|
|
|
|
|
|
|
from fields.base import ResponseModel
|
2026-04-16 16:50:02 +08:00
|
|
|
from graphon.variables.types import SegmentType
|
2026-05-14 19:34:31 +09:00
|
|
|
from libs.helper import TimestampField, to_timestamp
|
2024-08-13 14:44:10 +08:00
|
|
|
|
2025-07-16 12:31:37 +08:00
|
|
|
from ._value_type_serializer import serialize_value_type
|
|
|
|
|
|
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,
|
2025-07-16 12:31:37 +08:00
|
|
|
"value_type": fields.String(attribute=serialize_value_type),
|
2024-08-15 12:54:05 +08:00
|
|
|
"value": fields.String,
|
|
|
|
|
"description": fields.String,
|
|
|
|
|
"created_at": TimestampField,
|
|
|
|
|
"updated_at": TimestampField,
|
2024-08-13 14:44:10 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-25 09:26:54 +08:00
|
|
|
|
2026-04-15 21:39:30 -07:00
|
|
|
class ConversationVariableResponse(ResponseModel):
|
|
|
|
|
id: str
|
|
|
|
|
name: str
|
|
|
|
|
value_type: str
|
|
|
|
|
value: str | None = None
|
|
|
|
|
description: str | None = None
|
|
|
|
|
created_at: int | None = None
|
|
|
|
|
updated_at: int | None = None
|
|
|
|
|
|
|
|
|
|
@field_validator("value_type", mode="before")
|
|
|
|
|
@classmethod
|
|
|
|
|
def _normalize_value_type(cls, value: Any) -> str:
|
|
|
|
|
exposed_type = getattr(value, "exposed_type", None)
|
|
|
|
|
if callable(exposed_type):
|
2026-04-18 19:16:24 +08:00
|
|
|
return str(exposed_type())
|
2026-04-15 21:39:30 -07:00
|
|
|
if isinstance(value, str):
|
|
|
|
|
try:
|
2026-04-18 19:16:24 +08:00
|
|
|
return str(SegmentType(value).exposed_type())
|
2026-04-15 21:39:30 -07:00
|
|
|
except ValueError:
|
|
|
|
|
return value
|
|
|
|
|
try:
|
|
|
|
|
return serialize_value_type(value)
|
|
|
|
|
except (AttributeError, TypeError, ValueError):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
return serialize_value_type({"value_type": value})
|
|
|
|
|
except (AttributeError, TypeError, ValueError):
|
|
|
|
|
value_attr = getattr(value, "value", None)
|
|
|
|
|
if value_attr is not None:
|
|
|
|
|
return str(value_attr)
|
|
|
|
|
return str(value)
|
|
|
|
|
|
|
|
|
|
@field_validator("value", mode="before")
|
|
|
|
|
@classmethod
|
|
|
|
|
def _normalize_value(cls, value: Any | None) -> str | None:
|
|
|
|
|
if value is None:
|
|
|
|
|
return None
|
|
|
|
|
if isinstance(value, str):
|
|
|
|
|
return value
|
|
|
|
|
return str(value)
|
|
|
|
|
|
|
|
|
|
@field_validator("created_at", "updated_at", mode="before")
|
|
|
|
|
@classmethod
|
|
|
|
|
def _normalize_timestamp(cls, value: datetime | int | None) -> int | None:
|
2026-05-14 19:34:31 +09:00
|
|
|
return to_timestamp(value)
|
2026-04-15 21:39:30 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class PaginatedConversationVariableResponse(ResponseModel):
|
|
|
|
|
page: int
|
|
|
|
|
limit: int
|
|
|
|
|
total: int
|
|
|
|
|
has_more: bool
|
|
|
|
|
data: list[ConversationVariableResponse]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConversationVariableInfiniteScrollPaginationResponse(ResponseModel):
|
|
|
|
|
limit: int
|
|
|
|
|
has_more: bool
|
|
|
|
|
data: list[ConversationVariableResponse]
|