from __future__ import annotations from uuid import uuid4 def resolve_idempotency_key(raw: dict, headers: dict[str, str], channel_type: str) -> str: header_key = headers.get("idempotency-key", "") if header_key: return header_key if channel_type == "feishu": return raw.get("header", {}).get("event_id", str(uuid4())) if channel_type == "dingtalk": return raw.get("msgId", str(uuid4())) trace_id = raw.get("trace_id", headers.get("x-trace-id", "")) return trace_id or str(uuid4())