Skip to main content

a3s_code_core/agent_api/
run_facade.rs

1use super::*;
2
3impl AgentSession {
4    /// Cancel a specific run only if it is still the active run.
5    ///
6    /// This is useful for SDK callers that hold a previously observed run ID:
7    /// stale run IDs will not cancel a newer operation.
8    pub async fn cancel_run(&self, run_id: &str) -> bool {
9        RunControl::from_session(self).cancel_run(run_id).await
10    }
11
12    /// Return snapshots for runs recorded by this session.
13    pub async fn runs(&self) -> Vec<crate::run::RunSnapshot> {
14        RunControl::from_session(self).runs().await
15    }
16
17    /// Return a snapshot for a recorded run.
18    pub async fn run_snapshot(&self, run_id: &str) -> Option<crate::run::RunSnapshot> {
19        RunControl::from_session(self).run_snapshot(run_id).await
20    }
21
22    /// Return recorded runtime events for a run.
23    pub async fn run_events(&self, run_id: &str) -> Vec<crate::run::RunEventRecord> {
24        RunControl::from_session(self).run_events(run_id).await
25    }
26
27    /// Return a cursor-based page from the run's retained event window.
28    ///
29    /// `after_sequence` is exclusive. The result is `None` for an unknown run;
30    /// a known run with no retained events returns an empty page. Inspect
31    /// `retention_gap` before treating the page as complete history.
32    pub async fn run_event_page(
33        &self,
34        run_id: &str,
35        after_sequence: Option<usize>,
36        limit: usize,
37    ) -> Option<crate::run::RunEventPage> {
38        RunControl::from_session(self)
39            .run_event_page(run_id, after_sequence, limit)
40            .await
41    }
42
43    /// Return a handle for the currently running operation, if any.
44    pub async fn current_run(&self) -> Option<crate::run::RunHandle> {
45        RunControl::from_session(self).current_run().await
46    }
47
48    /// Return active tool calls observed for the currently running operation.
49    pub async fn active_tools(&self) -> Vec<crate::run::ActiveToolSnapshot> {
50        SessionView::from_session(self).active_tools().await
51    }
52
53    /// Look up a delegated subagent task by id. Returns `None` if no such task
54    /// has been observed in this session.
55    pub async fn subagent_task(
56        &self,
57        task_id: &str,
58    ) -> Option<crate::subagent_task_tracker::SubagentTaskSnapshot> {
59        self.subagent_tasks.get(task_id).await
60    }
61
62    /// Return snapshots of every delegated subagent task observed in this
63    /// session (including completed and failed ones), oldest first.
64    pub async fn subagent_tasks(&self) -> Vec<crate::subagent_task_tracker::SubagentTaskSnapshot> {
65        self.subagent_tasks.list_for_parent(&self.session_id).await
66    }
67
68    /// Return snapshots of subagent tasks still in `Running` state.
69    pub async fn pending_subagent_tasks(
70        &self,
71    ) -> Vec<crate::subagent_task_tracker::SubagentTaskSnapshot> {
72        use crate::subagent_task_tracker::SubagentStatus;
73        self.subagent_tasks
74            .list_for_parent(&self.session_id)
75            .await
76            .into_iter()
77            .filter(|task| task.status == SubagentStatus::Running)
78            .collect()
79    }
80
81    /// Cancel an in-flight delegated subagent task by id. Returns `true`
82    /// when a cancellation token was found and fired, `false` when the
83    /// task id is unknown or the task has already finished. The eventual
84    /// `SubagentEnd` from the cancelled child loop won't downgrade the
85    /// terminal status — it stays `Cancelled`.
86    pub async fn cancel_subagent_task(&self, task_id: &str) -> bool {
87        self.subagent_tasks.cancel(task_id).await
88    }
89
90    /// Return a shared handle to the session's subagent task tracker.
91    ///
92    /// Advanced: embedders implementing a custom subagent execution path
93    /// (i.e. spawning child loops outside the built-in `task` tool) can use
94    /// this to register cancellation tokens and feed `AgentEvent`s into the
95    /// tracker so the standard
96    /// [`subagent_task`](Self::subagent_task) / [`pending_subagent_tasks`](Self::pending_subagent_tasks) /
97    /// [`cancel_subagent_task`](Self::cancel_subagent_task) APIs and
98    /// [`close`](Self::close) keep working uniformly across execution paths.
99    pub fn subagent_tracker(
100        &self,
101    ) -> Arc<crate::subagent_task_tracker::InMemorySubagentTaskTracker> {
102        Arc::clone(&self.subagent_tasks)
103    }
104}