feat(rectification): pass ayanamsa per request

This commit is contained in:
Jesse_Chen
2026-09-04 00:25:16 +08:00
parent 3b09bbbee0
commit 44d3d3931b
15 changed files with 135 additions and 16 deletions
+18 -1
View File
@@ -6,6 +6,8 @@ import uuid
from datetime import date
from typing import Any, Literal, NotRequired, TypedDict, cast
from scripts.ayanamsa_utils import UnsupportedAyanamsaError, normalize_ayanamsa_name
DatePrecision = Literal["day", "month", "quarter", "year", "range"]
EVENT_CONTRACT_VERSION = "rectification-event-contract-v2"
@@ -48,7 +50,10 @@ _EVENT_PROVENANCE_FIELDS = frozenset({
"date_source", "date_reliability", "date_corroboration", "date_conflict_status",
"source_turn_id", "subject",
})
_REQUEST_FIELDS = frozenset({"birth_date", "start_time", "end_time", "lat", "lon", "tz", "events"}) | _REQUEST_PROVENANCE_FIELDS
_REQUEST_FIELDS = frozenset({
"birth_date", "start_time", "end_time", "lat", "lon", "tz", "events",
"ayanamsa", "node_mode",
}) | _REQUEST_PROVENANCE_FIELDS
_EVENT_FIELDS = frozenset({"id", "domain", "event_kind", "date_start", "date_end", "precision", "summary"}) | _EVENT_PROVENANCE_FIELDS
_CLOCK = re.compile(r"(?:[01]\d|2[0-3]):[0-5]\d\Z")
@@ -77,6 +82,8 @@ class RectificationRequest(TypedDict):
lon: float
tz: float
events: list[LifeEvent]
ayanamsa: NotRequired[str]
node_mode: NotRequired[str]
birth_time_source: NotRequired[str | None]
timezone_id: NotRequired[str | None]
timezone_source: NotRequired[str | None]
@@ -227,6 +234,16 @@ def normalize_rectification_request(body: Any, *, today: date | None = None) ->
"tz": _bounded_number(body, "tz", -14, 14),
"events": cleaned_events,
}
if "ayanamsa" in body:
try:
cleaned_request["ayanamsa"] = normalize_ayanamsa_name(body.get("ayanamsa"))
except UnsupportedAyanamsaError as exc:
raise ValueError(str(exc)) from exc
if "node_mode" in body:
node_mode = body.get("node_mode")
if not isinstance(node_mode, str) or node_mode.strip().lower() not in {"mean", "true"}:
raise ValueError("node_mode must be mean or true")
cleaned_request["node_mode"] = node_mode.strip().lower()
_copy_nullable_text(body, cleaned_request, "birth_time_source", "birth_time_source", 120, _BIRTH_TIME_SOURCES)
_copy_nullable_text(body, cleaned_request, "timezone_id", "timezone_id", 120)
_copy_nullable_text(body, cleaned_request, "timezone_source", "timezone_source", 80)
+8 -2
View File
@@ -103,7 +103,7 @@ def sample_event_dates(event: LifeEvent) -> list[str]:
def _legacy_request(request: RectificationRequest, event: LifeEvent, sampled_date: str) -> dict[str, Any]:
engine_domain, _ = _ENGINE_KIND_BY_NATIVE_KIND[event["event_kind"]]
return {
legacy_request = {
"birth_date": request["birth_date"],
"start_time": request["start_time"],
"end_time": request["end_time"],
@@ -116,6 +116,10 @@ def _legacy_request(request: RectificationRequest, event: LifeEvent, sampled_dat
"date": sampled_date, "precision": "day", "summary": event.get("summary", ""),
}],
}
for key in ("ayanamsa", "node_mode"):
if key in request:
legacy_request[key] = request[key]
return legacy_request
def _canonical(value: Any) -> str:
@@ -349,7 +353,9 @@ def calculation_spec(request: RectificationRequest) -> dict[str, Any]:
"latitude": json_number(request["lat"]),
"longitude": json_number(request["lon"]),
"timezoneOffsetHours": json_number(request["tz"]),
"ayanamsa": "raman", "nodeMode": "mean", "minuteStep": 1,
"ayanamsa": request.get("ayanamsa", "raman"),
"nodeMode": request.get("node_mode", "mean"),
"minuteStep": 1,
}
for source, target in (
("birth_time_source", "birthTimeSource"),