Skip to main content

codex_extension_api/capabilities/
agent.rs

1use std::future::Future;
2use std::pin::Pin;
3
4use codex_protocol::ThreadId;
5
6/// Future returned by one injected subagent-spawn helper.
7pub type AgentSpawnFuture<'a, T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'a>>;
8
9/// Constructor-injected host helper for extensions that need to spawn subagents.
10///
11/// The extension owns the request shape and resulting handle types. The host
12/// provides the implementation when it constructs the extension.
13pub trait AgentSpawner<R>: Send + Sync {
14    type Spawned;
15    type Error;
16
17    fn spawn_subagent<'a>(
18        &'a self,
19        forked_from_thread_id: ThreadId,
20        request: R,
21    ) -> AgentSpawnFuture<'a, Self::Spawned, Self::Error>;
22}
23
24impl<R, S, E, F> AgentSpawner<R> for F
25where
26    F: Fn(ThreadId, R) -> AgentSpawnFuture<'static, S, E> + Send + Sync,
27{
28    type Spawned = S;
29    type Error = E;
30
31    fn spawn_subagent<'a>(
32        &'a self,
33        forked_from_thread_id: ThreadId,
34        request: R,
35    ) -> AgentSpawnFuture<'a, Self::Spawned, Self::Error> {
36        self(forked_from_thread_id, request)
37    }
38}