Skip to content

codenib.agent.skills.context

Typed skill-context layer.

Skill executors are factories of the shape create_executor(context) -> Callable. Historically the context argument was annotated Any in every skill and the loader selected it via a stringly-typed dict lookup (_CONTEXT_KEY_FOR_TYPE), so the concrete, well-typed dataclasses defined in codenib/ops/ (RetrieveContext, RerankContext, TransformContext, ExpandContext) were erased to Any the moment they crossed into a skill.

This module makes the boundary typed:

  • :data:SkillContext — the union of the four per-op context dataclasses; the type a single-context skill's create_executor should accept.
  • :class:ComposerContexts — a typed aggregate for multi-context (custom) composer skills (codenib_context, bm25_search) that genuinely need more than one context. Replaces the untyped Dict[str, Any] they took.
  • :data:CONTEXT_KEY_FOR_TYPESkillType -> context-dict key mapping, keyed on the enum (not a bare string), used by the loader to pick the single context a skill receives.

The dataclasses live in ops/ (they carry behaviour like ensure_agent); this module only re-exports them as a typed surface so the agent layer can annotate against them without re-importing ops everywhere.

Classes:

Name Description
ComposerContexts

Typed bundle of per-op contexts for multi-context composer skills.

ComposerContexts dataclass

ComposerContexts(
    retrieve: "RetrieveContext" | None = None,
    expand: "ExpandContext" | None = None,
    rerank: "RerankContext" | None = None,
    transform: "TransformContext" | None = None,
    cross_encoder: "CrossEncoderContext" | None = None,
)

Typed bundle of per-op contexts for multi-context composer skills.

custom skills that compose several stages (search + graph expand, e.g. codenib_context and the names-mode of bm25_search) need more than one context. They used to receive the loader's raw Dict[str, Any] and reach into it with .get("retrieve") / getattr(..., None). This dataclass gives that bundle a name and a type; any field may be None when the corresponding index/context was not built.

Fields mirror the keys produced by codenib.compiler.skill_context._package_contexts.

Methods:

Name Description
from_mapping

Build from the loader's {key: context} mapping (or None).

from_mapping classmethod

from_mapping(contexts: Mapping[str, object] | None) -> 'ComposerContexts'

Build from the loader's {key: context} mapping (or None).

Source code in codenib/agent/skills/context.py
@classmethod
def from_mapping(
    cls, contexts: Optional[Mapping[str, object]]
) -> "ComposerContexts":
    """Build from the loader's ``{key: context}`` mapping (or ``None``)."""
    d = contexts or {}
    return cls(
        retrieve=d.get("retrieve"),  # type: ignore[arg-type]
        expand=d.get("expand"),  # type: ignore[arg-type]
        rerank=d.get("rerank"),  # type: ignore[arg-type]
        transform=d.get("transform"),  # type: ignore[arg-type]
        cross_encoder=d.get("cross_encoder"),  # type: ignore[arg-type]
    )