|
5 | 5 | system once and re-exports the agent instances under their historical names so |
6 | 6 | existing imports keep working. |
7 | 7 | """ |
| 8 | +import copy |
| 9 | +import logging |
| 10 | + |
8 | 11 | from CoScientist.assembly import build_system |
9 | | -from CoScientist.logging import multi_agent_tracer |
| 12 | +from CoScientist.assembly.schema import load_config |
| 13 | +from CoScientist.logging import get_multi_agent_tracer |
10 | 14 | from CoScientist.agents.llm_repair import install_json_repair |
| 15 | +from opik.integrations.adk import track_adk_agent_recursive |
| 16 | + |
| 17 | +logger = logging.getLogger(__name__) |
11 | 18 |
|
12 | 19 | # Guard the LiteLlm tool-call JSON boundary process-wide BEFORE any runner executes: |
13 | 20 | # a malformed tool-call payload (qwen truncation / missing comma) must not kill the run. |
|
32 | 39 | root_agent = orchestrator_agent |
33 | 40 |
|
34 | 41 | # Agents that run as pipeline stages (pre/post) around the orchestrator. |
35 | | -pipeline_pre_agents = [_system.agent(n) for n in _system.config.pipeline.pre] |
36 | | -pipeline_post_agents = [_system.agent(n) for n in _system.config.pipeline.post] |
| 42 | +pipeline_pre_agents = [_system.agent(n) for n in _system.config.pipeline.pre if _system.config.agent(n).is_enabled()] |
| 43 | +pipeline_post_agents = [_system.agent(n) for n in _system.config.pipeline.post if _system.config.agent(n).is_enabled()] |
37 | 44 |
|
38 | 45 | # The RUN root: the whole lifecycle (pre → orchestrator → post/aggregator) is one |
39 | 46 | # ADK SequentialAgent, driven by a single Runner.run_async so it is ONE invocation |
40 | 47 | # = ONE trace, with the Result Aggregator as the terminal child (it reads the graph |
41 | 48 | # the orchestrator populated and writes the report). When no pipeline stages are |
42 | 49 | # declared, the orchestrator IS the run root (no needless wrapper). |
43 | | -if pipeline_pre_agents or pipeline_post_agents: |
44 | | - from google.adk.agents.sequential_agent import SequentialAgent |
45 | | - |
46 | | - run_root = SequentialAgent( |
47 | | - name="ResearchPipeline", |
48 | | - description="Full research lifecycle: orchestrator run then report synthesis.", |
49 | | - sub_agents=[*pipeline_pre_agents, orchestrator_agent, *pipeline_post_agents], |
50 | | - ) |
51 | | -else: |
52 | | - run_root = orchestrator_agent |
| 50 | +run_root = _system.run_root |
53 | 51 |
|
54 | 52 | planner_agent = _system.agents.get("PlannerAgent") |
55 | 53 | hypotheses_agent = _system.agents.get("HypothesesAgent") |
|
66 | 64 | tz_agent = _system.agents.get("TZAgent") |
67 | 65 |
|
68 | 66 | # Attach the Opik tracer only when tracing is enabled (see OPIK__ENABLED). |
69 | | -# Tracking the run root covers the whole SequentialAgent (orchestrator + pipeline |
70 | | -# stages) so the entire lifecycle lands in ONE trace. |
71 | | -if multi_agent_tracer is not None: |
72 | | - from opik.integrations.adk import track_adk_agent_recursive |
| 67 | +_tracer = get_multi_agent_tracer() |
| 68 | +if _tracer is not None: |
| 69 | + track_adk_agent_recursive(run_root, _tracer) |
| 70 | + |
| 71 | + |
| 72 | +def build_for_mode(): |
| 73 | + """Build an AgentSystem configured for the current start mode from settings. |
| 74 | +
|
| 75 | + Reads ``settings.web.start_mode``: |
| 76 | + * ``"planner"`` — PlanningPipelineAgent is root (sequential: PlannerAgent → |
| 77 | + OrchestratorAgent). |
| 78 | + * ``"orchestrator"`` — OrchestratorAgent is root, with PlannerAgent |
| 79 | + added to its subordinates so it can be invoked on demand. |
| 80 | + * ``"orchestrator_planner"`` — OrchestratorAgent is root, provided with |
| 81 | + create_plan_tool directly, while PlannerAgent is disabled. |
| 82 | +
|
| 83 | + Other runtime-tunable parameters (e.g. ``max_searches``) are read from |
| 84 | + ``settings.web`` by individual components at build time. |
| 85 | +
|
| 86 | + Returns: |
| 87 | + An :class:`~CoScientist.assembly.assembler.AgentSystem`. |
| 88 | + """ |
| 89 | + from CoScientist.config import get_settings |
| 90 | + start_mode = get_settings().web.start_mode |
| 91 | + |
| 92 | + if start_mode in ("planner"): |
| 93 | + raw_config = load_config() |
| 94 | + patched = copy.deepcopy(raw_config) |
| 95 | + pipeline_agent_name = "PlanningPipelineAgent" if "PlanningPipelineAgent" in patched.agents else "InitAgent" |
| 96 | + if pipeline_agent_name in patched.agents: |
| 97 | + patched.agents[pipeline_agent_name].root = True |
| 98 | + patched.agents[pipeline_agent_name].enabled = True |
| 99 | + patched.agents["OrchestratorAgent"].root = False |
| 100 | + # In Planner mode the PlannerAgent runs first and its output replaces |
| 101 | + # the original user query; inject_original_query restores it so the |
| 102 | + # OrchestratorAgent sees the original request. |
| 103 | + orch_cb = patched.agents["OrchestratorAgent"].callbacks.before_model |
| 104 | + if "inject_original_query" not in orch_cb: |
| 105 | + orch_cb.append("inject_original_query") |
| 106 | + system = build_system(config=patched) |
| 107 | + else: |
| 108 | + logger.warning( |
| 109 | + "start_mode is set to %r but 'PlanningPipelineAgent' is not present in " |
| 110 | + "the system config; falling back to default build_system()", |
| 111 | + start_mode, |
| 112 | + ) |
| 113 | + system = build_system() |
| 114 | + _tracer = get_multi_agent_tracer() |
| 115 | + if _tracer is not None: |
| 116 | + track_adk_agent_recursive(system.run_root, _tracer) |
| 117 | + return system |
| 118 | + |
| 119 | + if start_mode in ("orchestrator_planner", "orchestrator_plan"): |
| 120 | + raw_config = load_config() |
| 121 | + patched = copy.deepcopy(raw_config) |
| 122 | + |
| 123 | + # Make OrchestratorAgent the root. |
| 124 | + patched.agents["OrchestratorAgent"].root = True |
| 125 | + for name in ("PlanningPipelineAgent", "InitAgent"): |
| 126 | + if name in patched.agents: |
| 127 | + patched.agents[name].root = False |
| 128 | + patched.agents[name].enabled = False |
| 129 | + |
| 130 | + # Disable PlannerAgent and remove from Orchestrator's subordinates. |
| 131 | + if "PlannerAgent" in patched.agents: |
| 132 | + patched.agents["PlannerAgent"].root = False |
| 133 | + patched.agents["PlannerAgent"].enabled = False |
| 134 | + |
| 135 | + orch_subs = patched.agents["OrchestratorAgent"].subordinates |
| 136 | + if "PlannerAgent" in orch_subs: |
| 137 | + orch_subs.remove("PlannerAgent") |
| 138 | + |
| 139 | + # Give OrchestratorAgent the tool for creating/registering plans directly. |
| 140 | + orch_tools = patched.agents["OrchestratorAgent"].tools |
| 141 | + if "create_plan_tool" not in orch_tools: |
| 142 | + orch_tools.append("create_plan_tool") |
| 143 | + |
| 144 | + system = build_system(config=patched) |
| 145 | + _tracer = get_multi_agent_tracer() |
| 146 | + if _tracer is not None: |
| 147 | + track_adk_agent_recursive(system.run_root, _tracer) |
| 148 | + return system |
| 149 | + |
| 150 | + if start_mode != "orchestrator": |
| 151 | + raise ValueError( |
| 152 | + f"Unknown start_mode {start_mode!r}; expected 'planner', 'orchestrator', or 'orchestrator_planner'" |
| 153 | + ) |
| 154 | + |
| 155 | + # Load a fresh config and patch it for orchestrator-as-root mode. |
| 156 | + raw_config = load_config() |
| 157 | + patched = copy.deepcopy(raw_config) |
| 158 | + |
| 159 | + # Make OrchestratorAgent the root. |
| 160 | + patched.agents["OrchestratorAgent"].root = True |
| 161 | + for name in ("PlanningPipelineAgent", "InitAgent"): |
| 162 | + if name in patched.agents: |
| 163 | + patched.agents[name].root = False |
| 164 | + patched.agents[name].enabled = False |
| 165 | + |
| 166 | + # Add PlannerAgent to OrchestratorAgent's subordinates (if not already). |
| 167 | + orch_subs = patched.agents["OrchestratorAgent"].subordinates |
| 168 | + if "PlannerAgent" not in orch_subs: |
| 169 | + orch_subs.insert(0, "PlannerAgent") |
73 | 170 |
|
74 | | - track_adk_agent_recursive(run_root, multi_agent_tracer) |
| 171 | + # Re-validate the patched config and build. |
| 172 | + system = build_system(config=patched) |
| 173 | + _tracer = get_multi_agent_tracer() |
| 174 | + if _tracer is not None: |
| 175 | + track_adk_agent_recursive(system.run_root, _tracer) |
| 176 | + return system |
75 | 177 |
|
76 | 178 | __all__ = [ |
77 | 179 | "agent_system", |
|
93 | 195 | "pipeline_pre_agents", |
94 | 196 | "pipeline_post_agents", |
95 | 197 | "tz_agent", |
| 198 | + "build_for_mode", |
96 | 199 | ] |
0 commit comments