Skip to main content

lc_agents/orchestrator/
task_adapter.rs

1//! `TaskAdapter`: bridges an `Input=String` orchestrator into a child agent consuming [`AgentTask`] (P2-5).
2
3use async_trait::async_trait;
4use std::sync::Arc;
5
6use super::{Orchestrator, RunContext};
7use crate::task::AgentTask;
8use crate::AgentError;
9
10/// Adapts an `Input=String` orchestrator into a child agent consuming [`AgentTask`] (P2-5).
11///
12/// Bridges two orchestrator kinds: a real agent (PlanExecuteAgent /
13/// DeepResearchAgent, etc., `Input=String`) wrapped this way can be placed in a
14/// `FanOutFanIn` / `SequentialPipeline` that dispatches [`AgentTask`]. Takes the
15/// objective and feeds it to the inner orchestrator; when the task declares
16/// `allowed_tools`, the consumer (AgentExecutor, etc.) assembles the tool list
17/// from that allowlist — this adapter does not filter on its behalf.
18pub struct TaskAdapter {
19    inner: Arc<dyn Orchestrator<Input = String, Output = String>>,
20}
21
22impl TaskAdapter {
23    /// Wrap an `Input=String` orchestrator.
24    pub fn new(inner: Arc<dyn Orchestrator<Input = String, Output = String>>) -> Self {
25        Self { inner }
26    }
27}
28
29#[async_trait]
30impl Orchestrator for TaskAdapter {
31    type Input = AgentTask;
32    type Output = String;
33
34    async fn run_with_context(
35        &self,
36        task: Self::Input,
37        ctx: &RunContext,
38    ) -> Result<Self::Output, AgentError> {
39        log::debug!(
40            target: "lc_agents::orchestrator",
41            "TaskAdapter dispatch objective='{}' trace_id = {}",
42            task.objective,
43            ctx.trace_id
44        );
45        self.inner.run_with_context(task.objective, ctx).await
46    }
47}
48
49/// Convenience wrapper: converts an `Input=String` orchestrator into a trait object that accepts [`AgentTask`] dispatch.
50pub fn task_adapter(
51    inner: Arc<dyn Orchestrator<Input = String, Output = String>>,
52) -> Arc<dyn Orchestrator<Input = AgentTask, Output = String>> {
53    Arc::new(TaskAdapter::new(inner))
54}