feat(runner): 注入 tool_dispatch_factory/prompt_builder_factory + fail-fast
Runner.__init__ 新增 2 个可选参数: - tool_dispatch_factory: 工具调度工厂 - prompt_builder_factory: prompt 构建工厂 infer/eval/train 模式缺少工厂时 fail-fast 抛 ValueError。 _make_tool_dispatch_fn/_make_prompt_builder 优先使用注入工厂。 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
+28
-6
@@ -449,6 +449,8 @@ class Runner:
|
||||
evolve_llm: 进化用 LLMProvider(thinking=True)。
|
||||
vlm: VLMProvider。
|
||||
telemetry: 遥测记录端口。
|
||||
tool_dispatch_factory: 工具调度工厂(infer/eval/train 模式必传)。
|
||||
prompt_builder_factory: prompt 构建工厂(infer/eval/train 模式必传)。
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
@@ -459,12 +461,28 @@ class Runner:
|
||||
evolve_llm: LLMProvider,
|
||||
vlm: VLMProvider,
|
||||
telemetry: TelemetryRecorder,
|
||||
tool_dispatch_factory: Any | None = None,
|
||||
prompt_builder_factory: Any | None = None,
|
||||
) -> None:
|
||||
self._config = config
|
||||
self._llm = llm
|
||||
self._evolve_llm = evolve_llm
|
||||
self._vlm = vlm
|
||||
self._telemetry = telemetry
|
||||
self._tool_dispatch_factory = tool_dispatch_factory
|
||||
self._prompt_builder_factory = prompt_builder_factory
|
||||
|
||||
# fail-fast: 需要推理的模式必须注入工厂
|
||||
if config.mode in {"infer", "eval", "train"} and (
|
||||
tool_dispatch_factory is None or prompt_builder_factory is None
|
||||
):
|
||||
raise ValueError(
|
||||
f"mode={config.mode!r} 需要 tool_dispatch_factory 和 "
|
||||
f"prompt_builder_factory,但收到 "
|
||||
f"tool_dispatch_factory={tool_dispatch_factory!r}, "
|
||||
f"prompt_builder_factory={prompt_builder_factory!r}"
|
||||
)
|
||||
|
||||
self._ensure_workspace()
|
||||
self._paths: ResolvedPaths = resolve_paths(config.workspace_dir)
|
||||
|
||||
@@ -2062,22 +2080,26 @@ class Runner:
|
||||
# -----------------------------------------------------------------------
|
||||
|
||||
def _make_tool_dispatch_fn(self, *, skills_dir: Path | None = None):
|
||||
"""构造工具调度函数(由子类或 main.py 覆盖)。"""
|
||||
"""构造工具调度函数(优先使用注入的工厂,否则 noop 降级)。"""
|
||||
if self._tool_dispatch_factory is not None:
|
||||
return self._tool_dispatch_factory(skills_dir=skills_dir)
|
||||
|
||||
# noop fallback:diagnose/promote 等不需要推理的模式
|
||||
async def _noop_dispatch(tool_name: str, args: dict, *, context: dict) -> str:
|
||||
raise NotImplementedError(
|
||||
f"工具 {tool_name} 调度未配置(需由 main.py 注入 tool_dispatch_fn)"
|
||||
)
|
||||
raise NotImplementedError(f"工具 {tool_name} 调度未配置")
|
||||
|
||||
return _noop_dispatch
|
||||
|
||||
def _make_prompt_builder(
|
||||
self, *, skills_dir: Path | None = None, prompts_dir: Path | None = None
|
||||
):
|
||||
"""构造 prompt 构建函数(由子类或 main.py 覆盖)。"""
|
||||
"""构造 prompt 构建函数(优先使用注入的工厂,否则 noop 降级)。"""
|
||||
if self._prompt_builder_factory is not None:
|
||||
return self._prompt_builder_factory(skills_dir=skills_dir, prompts_dir=prompts_dir)
|
||||
|
||||
# noop fallback:diagnose/promote 等不需要推理的模式
|
||||
def _noop_builder(qa: GeneratedQuestion) -> tuple[str, str]:
|
||||
raise NotImplementedError("prompt_builder 未配置(需由 main.py 注入)")
|
||||
raise NotImplementedError("prompt_builder 未配置")
|
||||
|
||||
return _noop_builder
|
||||
|
||||
|
||||
Reference in New Issue
Block a user