fix(dispatch): publish to /dispatch/envelopes with event-card profile (doc 23)
The client posted a body-only envelope to {DISPATCH_API_URL}/messages — an
endpoint that does not exist. Now: /dispatch/envelopes, mandatory DispatchCard
block per the platform pydantic model (int schema_version=1/state_code=0,
task_id=0, int4-safe epoch-second sequence_number), payload under card.event,
UUID card_uid (stable uuid5 for ULID-style asset ids). Inbox accepts both
card.event and legacy root body.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,7 @@ Design notes:
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
@@ -40,6 +41,16 @@ def deterministic_event_id(*parts: Any) -> str:
|
||||
return str(uuid.uuid5(_DISPATCH_NAMESPACE, ":".join(str(p) for p in parts)))
|
||||
|
||||
|
||||
def _card_uid(value: str) -> str:
|
||||
"""Envelope card_uid must be a UUID (dispatch_api/models.py). Business ids
|
||||
that are already UUIDs pass through; ULID-style asset ids map to a stable
|
||||
uuid5 so re-emits keep the same card."""
|
||||
try:
|
||||
return str(uuid.UUID(str(value)))
|
||||
except ValueError:
|
||||
return str(uuid.uuid5(_DISPATCH_NAMESPACE, str(value)))
|
||||
|
||||
|
||||
def build_envelope(
|
||||
message_type: str,
|
||||
card_uid: str,
|
||||
@@ -49,15 +60,37 @@ def build_envelope(
|
||||
message_id: str | None = None,
|
||||
created_at: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Build the dispatch JSON envelope (INTER_MODULE_CONTRACT §2.4)."""
|
||||
"""Build the dispatch JSON envelope (02 §2.4 + platform doc 23 profile).
|
||||
|
||||
The card block mirrors the mandatory DispatchCard fields of the platform
|
||||
pydantic model (dispatch_api/models.py): int schema_version/state_code,
|
||||
task_id 0 for out-of-ticket events, int4-safe epoch-second
|
||||
sequence_number; the event payload nests under card.event untouched.
|
||||
"""
|
||||
now = created_at or datetime.now(tz=UTC).isoformat()
|
||||
uid = _card_uid(card_uid)
|
||||
return {
|
||||
"message_id": message_id or str(uuid.uuid4()),
|
||||
"event_id": event_id or str(uuid.uuid4()),
|
||||
"card_uid": str(card_uid),
|
||||
"card_uid": uid,
|
||||
"task_id": 0,
|
||||
"message_type": message_type,
|
||||
"participant_code": settings.dispatch_participant_code,
|
||||
"created_at": created_at or datetime.now(tz=UTC).isoformat(),
|
||||
"body": body,
|
||||
"created_at": now,
|
||||
"card": {
|
||||
"schema_version": 1,
|
||||
"card_uid": uid,
|
||||
"task_id": 0,
|
||||
"sequence_number": int(time.time()),
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
"state_code": 0,
|
||||
"specialist_status": "n/a",
|
||||
"event": body,
|
||||
},
|
||||
"media_files": [],
|
||||
"media_file_count": 0,
|
||||
"local_delivery_status": {},
|
||||
}
|
||||
|
||||
|
||||
@@ -80,7 +113,7 @@ def publish(
|
||||
envelope = build_envelope(message_type, card_uid, body, event_id=event_id)
|
||||
try:
|
||||
res = httpx.post(
|
||||
f"{settings.dispatch_api_url.rstrip('/')}/messages",
|
||||
f"{settings.dispatch_api_url.rstrip('/')}/dispatch/envelopes",
|
||||
json=envelope,
|
||||
headers={"X-API-Key": settings.dispatch_api_key},
|
||||
timeout=settings.dispatch_timeout_seconds,
|
||||
@@ -172,7 +205,10 @@ def handle_inbox_message(envelope: dict[str, Any]) -> dict[str, Any]:
|
||||
|
||||
event_id = str(envelope.get("event_id") or "")
|
||||
message_type = str(envelope.get("message_type") or "")
|
||||
body = envelope.get("body") or {}
|
||||
# Doc 23 profile nests the payload under card.event; legacy senders used a
|
||||
# root-level body. Accept both at this external boundary.
|
||||
card = envelope.get("card") or {}
|
||||
body = card.get("event") or envelope.get("body") or {}
|
||||
|
||||
with session_scope() as db:
|
||||
already = db.execute(
|
||||
|
||||
Reference in New Issue
Block a user