reducer path to strengthen too

This commit is contained in:
lda
2026-05-21 04:19:50 +07:00 Verified
parent 444b5735c1
commit d089e28c15
9 changed files with 201 additions and 38 deletions
+46 -3
View File
@@ -1,16 +1,59 @@
from __future__ import annotations
from collections.abc import Mapping
from typing import Any
from pydantic import BaseModel, ConfigDict, Field
from pydantic import BaseModel, ConfigDict, Field, model_validator
from wf_platform.refs import CapabilityRef
class ReducerRef(BaseModel):
"""Reference to one reducer plus JSON-compatible configuration."""
"""Reference to one reducer capability plus JSON-compatible configuration."""
name: str
ref: CapabilityRef
config: dict[str, Any] = Field(default_factory=dict)
def __init__(
self,
*,
ref: CapabilityRef | str | Mapping[str, Any] | None = None,
name: str | None = None,
config: dict[str, Any] | None = None,
**extra: object,
) -> None:
"""Accept legacy `name=` construction while storing canonical `ref`.
Existing authoring and runtime code still constructs reducers with
`ReducerRef(name="wf.std.add")`. That remains source-compatible, but
the model state is now the structural capability reference.
"""
payload: dict[str, object] = {"config": config or {}}
if ref is not None:
payload["ref"] = ref
if name is not None:
payload["name"] = name
payload.update(extra)
super().__init__(**payload)
@model_validator(mode="before")
@classmethod
def _coerce_legacy_shapes(cls, value: object) -> object:
"""Accept old reducer strings/`name` objects as parse-only shorthand."""
if isinstance(value, str):
return {"ref": CapabilityRef.parse(value)}
if not isinstance(value, Mapping):
return value
data = dict(value)
if "ref" not in data and "name" in data:
data["ref"] = CapabilityRef.parse(str(data.pop("name")))
return data
@property
def name(self) -> str:
"""Display/registry compatibility key for existing reducer catalogs."""
return str(self.ref)
class ReducerSpec(BaseModel):
"""Inspectable metadata for one named pure state reducer."""