* test: enforce Python import ownership boundaries * test: protect client import boundaries and rehome shared UI contracts * refactor: remove gateway and client dependency inversions * fix: detect dynamic imports in conditional expressions * refactor(tools): keep configuration contracts with their owners * docs: keep design guidance focused on ownership and checks * fix: preserve default threaded turn delivery
52 lines
1.7 KiB
Python
52 lines
1.7 KiB
Python
"""Shared contracts for session-bound automation sources."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Callable, Mapping
|
|
from dataclasses import dataclass, field
|
|
from typing import Any, cast
|
|
|
|
AUTOMATION_HISTORY_META = "_automation_turn"
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class AutomationTurnSpec:
|
|
"""Source-specific wiring for one session-bound automation turn type."""
|
|
|
|
kind: str
|
|
trigger_meta_key: str
|
|
legacy_history_meta_key: str | None = None
|
|
history_fields: Mapping[str, str] = field(default_factory=dict[str, str])
|
|
text_builder: Callable[[Mapping[str, Any]], str | None] | None = None
|
|
|
|
|
|
def automation_trigger(
|
|
metadata: Mapping[str, Any] | None,
|
|
spec: AutomationTurnSpec,
|
|
) -> dict[str, Any] | None:
|
|
"""Return source trigger metadata for *spec* when present."""
|
|
raw = (metadata or {}).get(spec.trigger_meta_key)
|
|
return cast(dict[str, Any], raw) if isinstance(raw, dict) else None
|
|
|
|
|
|
def automation_history_overrides_for_spec(
|
|
metadata: Mapping[str, Any] | None,
|
|
spec: AutomationTurnSpec,
|
|
) -> tuple[str | None, dict[str, Any]]:
|
|
"""Return hidden session-history text/metadata overrides for *spec*."""
|
|
trigger = automation_trigger(metadata, spec)
|
|
if not trigger:
|
|
return None, {}
|
|
|
|
details: dict[str, Any] = {"kind": spec.kind}
|
|
extra: dict[str, Any] = {AUTOMATION_HISTORY_META: details}
|
|
if spec.legacy_history_meta_key:
|
|
extra[spec.legacy_history_meta_key] = True
|
|
for history_key, trigger_key in spec.history_fields.items():
|
|
value = trigger.get(trigger_key)
|
|
extra[history_key] = value
|
|
details[history_key] = value
|
|
|
|
text = spec.text_builder(trigger) if spec.text_builder else None
|
|
return text, extra
|