diff --git a/src/schemas/models.py b/src/schemas/models.py index fdc004b..673a1df 100644 --- a/src/schemas/models.py +++ b/src/schemas/models.py @@ -1,13 +1,22 @@ -from pydantic import BaseModel, Field -from typing import Any, Literal, Optional +from pydantic import BaseModel, BeforeValidator, Field +from typing import Annotated, Any, Literal, Optional ClientMode = Literal["direct", "browser_addon"] + +def normalize_client_mode(value: Any) -> Any: + if value is None or (isinstance(value, str) and value.strip() == ""): + return "direct" + return value + + +NormalizedClientMode = Annotated[ClientMode, BeforeValidator(normalize_client_mode)] + class ProcessRequest_chat(BaseModel): sessionId: str = Field(..., max_length=64) timeStamp: str = Field(..., max_length=32) text: str = Field(...) - clientMode: ClientMode = "direct" + clientMode: NormalizedClientMode = "direct" needFormUpdate: bool = False useTextChunk: bool = False diff --git a/test/test_client_mode.py b/test/test_client_mode.py index a0764b9..1afa949 100644 --- a/test/test_client_mode.py +++ b/test/test_client_mode.py @@ -36,6 +36,18 @@ def test_unknown_client_mode_is_rejected(): ) +@pytest.mark.parametrize("client_mode", ["", " ", None]) +def test_empty_client_mode_defaults_to_direct(client_mode): + request = ProcessRequest_chat( + sessionId="session-1", + timeStamp="1", + text="你好", + clientMode=client_mode, + ) + + assert request.clientMode == "direct" + + @pytest.mark.parametrize( ("client_mode", "raw_code", "expected"), [