Coverage for app / inference / engines / _factory.py: 100%
31 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
1"""
2InferenceEngineFactory — creates the appropriate engine for each execution type.
4Usage::
6 engine = InferenceEngineFactory.create(
7 request.type, db=db, rules_list=request.rules
8 )
9 response = await engine.infer(ctx)
10 # or
11 async for frame in engine.infer_stream(ctx):
12 yield frame
13"""
15from __future__ import annotations
17from typing import TYPE_CHECKING, Any
19from app.inference.monitor import UsageAccumulator
20from app.inference.rules import RulesEnforcer
21from app.inference.tracer import InferenceTracer
23if TYPE_CHECKING:
24 from app.inference.engine import InferenceEngine
25 from app.models.spec import RuleSpec
28class InferenceEngineFactory:
29 """
30 Creates a fully wired ``InferenceEngine`` for the requested execution type.
32 Each call produces a fresh ``UsageAccumulator`` and ``RulesEnforcer``
33 so cross-request state is never shared.
34 """
36 @staticmethod
37 def create(
38 execution_type: str,
39 db: Any = None,
40 rules_list: "list[RuleSpec] | None" = None,
41 ) -> "InferenceEngine":
42 """
43 Instantiate and return the engine for ``execution_type``.
45 Args:
46 execution_type: One of "model", "agent", "pipeline", "graph",
47 "skill", or "worker".
48 db: SQLAlchemy session (required for agent/skill/graph/
49 worker/pipeline; may be None for model).
50 rules_list: Governance rules forwarded from svc-core. An empty
51 list disables all rule enforcement.
53 Raises:
54 ValueError: Unknown execution type.
55 """
56 enforcer = RulesEnforcer(rules_list or [])
57 monitor = UsageAccumulator()
58 tracer = InferenceTracer()
60 match execution_type:
61 case "model":
62 from app.inference.engines.model import ModelEngine
64 return ModelEngine(enforcer, monitor, tracer)
66 case "agent":
67 from app.inference.engines.agent import AgentEngine
69 return AgentEngine(db, enforcer, monitor, tracer)
71 case "skill":
72 if db is None:
73 raise ValueError(
74 "A database session is required for skill execution"
75 )
76 from app.inference.engines.skill import SkillEngine
78 return SkillEngine(db, enforcer, monitor, tracer)
80 case "graph":
81 from app.inference.engines.graph import GraphEngine
83 return GraphEngine(db, enforcer, monitor, tracer)
85 case "pipeline":
86 from app.inference.engines.pipeline import PipelineEngine
88 return PipelineEngine(db, enforcer, monitor, tracer)
90 case _:
91 raise ValueError(f"Unknown execution type: {execution_type!r}")