Source:
__
Local role:
Typed class-based abstraction wrapping the function-based builders in builders.py. Provides a stable override point for kind-specific generation behavior.
Big-picture role: Enables callers to compose or replace per-kind generation logic without touching the pipeline. Satisfies the pluggable builder requirement of the generation architecture (F-16).
Inheritance (ContextualBuilder):
ABC,Generic[TGenerated]
Parameters and fields:
kind: str— class-level attribute; must be declared on each concrete subclass
Methods (ContextualBuilder):
ensure_kind(context)— raisesValueErrorifcontext.kinddoes not matchself.kindbuild(context) -> TGenerated— abstract; must be implemented by subclasses
Concrete builders:
WorldContextualBuilder—kind = "world", delegates tobuild_world(context)ActorContextualBuilder—kind = "actor", delegates tobuild_actor(context)StrategyContextualBuilder—kind = "strategy", delegates tobuild_strategy(context)GoalContextualBuilder—kind = "goal", delegates tobuild_goal(context)PerceptionContextualBuilder—kind = "perception", delegates tobuild_perception(context)
Module-level helpers:
default_contextual_builders()— returns adict[str, ContextualBuilder]mapping all five built-in kind strings to their instancesbuild_with_context_builder(context, *, builders=None)— looks upcontext.kindin the builders dict and callsbuild(context); raisesValueErroron unknown kind
Notes:
- Every concrete
build()implementation must callensure_kindfirst to prevent silent wrong-kind generation. default_contextual_builders()returns a fresh dict each call — safe to mutate.
See also:
Customization pattern
class MyActorBuilder(ActorContextualBuilder):
def build(self, context):
self.ensure_kind(context)
ctx = context.copy_with(attributes={**context.attributes, "source": "custom"})
return build_actor(ctx)
builders = default_contextual_builders()
builders["actor"] = MyActorBuilder()
result = build_with_context_builder(context, builders=builders)
