base/interface/tool/context.rs
1//! `ToolContext`: everything a tool call may read about its session and turn.
2
3use super::*;
4
5#[derive(Clone)]
6#[non_exhaustive]
7pub struct ToolContext {
8 pub cwd: PathBuf,
9 pub session_id: String,
10 pub turn_no: u32,
11 pub cancel: CancellationToken,
12 /// The engine's configuration; the session's sandbox settings are its
13 /// `sandbox`.
14 pub config: Arc<crate::context::EngineConfig>,
15 /// The session the call is made in: its live permission mode, the
16 /// directories it may write besides its own, its running tasks.
17 pub session: Arc<dyn crate::context::Session>,
18 pub tool_use_id: String,
19 /// How many delegations deep the calling session is; `0` for the
20 /// conversation the user talks to.
21 pub agent_depth: u32,
22 /// How a tool asks the user something — see
23 /// [`crate::interface::elicitation::Elicitation`]. `None` where nothing
24 /// can reach a person (a test context, a background run), which every
25 /// asking tool must read as a refusal rather than as permission to make
26 /// the answer up.
27 pub elicitation: Option<Arc<dyn crate::interface::elicitation::Elicitation>>,
28 /// How a host-declared tool reaches the host; `None` where no host can
29 /// answer, which such a tool reads as failing at once.
30 pub host_calls: Option<Arc<dyn crate::interface::host_call::HostCalls>>,
31 /// How this tool reaches the machine — see
32 /// [`crate::interface::exec`]. In production this machine's
33 /// (`platform`); a deployment executing somewhere else replaces the set
34 /// and no tool notices.
35 pub exec: crate::interface::exec::ExecProviders,
36 /// How this tool calls other tools — set only for a tool whose
37 /// [`ToolSpec::needs_dispatch`] says it does.
38 pub dispatch: Option<Arc<dyn crate::interface::tool_dispatch::ToolDispatch>>,
39 /// The call comes from a program (`RunCode`), not from the model: there
40 /// is nobody to ask and no conversation to report back to mid-call.
41 pub from_code: bool,
42 /// Who made the call, as `tool.around` is told: `model`, `user`,
43 /// `skill`, `RunCode`, `host`, or the plugin command or function whose
44 /// call it is.
45 pub via: String,
46 /// What the session's sandbox holds a command to.
47 pub confinement: crate::interface::exec::ConfinementReport,
48 /// How the engine runs its own git and helpers for the session — see
49 /// [`crate::interface::exec::SandboxSettings::engine_git`].
50 pub engine_git: Arc<crate::engine_exec::EngineExec>,
51 /// Whether someone can be asked about what this call does.
52 pub attendance: crate::interface::permission::Attendance,
53 /// What the policy let this call through on; `None` when nothing judged it.
54 pub approval: Option<crate::interface::permission::Approval>,
55 pub turn: Arc<TurnScope>,
56 /// How a skill's `` !`cmd` `` expanded during this call runs; `None`
57 /// where nothing runs them.
58 pub inline_commands: Option<Arc<dyn crate::interface::inline_command::InlineCommands>>,
59 /// What the permission rules in force deny of a directory walk; `None`
60 /// where nothing judges paths.
61 pub path_denials: Option<Arc<dyn crate::interface::permission::PathDenials>>,
62}
63
64/// What belongs to the one turn a call is made in, and ends with it.
65#[derive(Debug, Default)]
66pub struct TurnScope {
67 /// Who started the turn, which decides what goal operations it may perform.
68 pub origin: crate::context::goal::TurnOrigin,
69 structured_output: std::sync::Mutex<Option<Value>>,
70 spend: Arc<crate::interface::budget_policy::SpendNode>,
71}
72
73impl TurnScope {
74 pub fn new(origin: crate::context::goal::TurnOrigin) -> Self {
75 Self {
76 origin,
77 structured_output: Default::default(),
78 spend: Arc::default(),
79 }
80 }
81
82 /// A turn whose calls spend into `spend`.
83 pub fn opening(
84 origin: crate::context::goal::TurnOrigin,
85 spend: Arc<crate::interface::budget_policy::SpendNode>,
86 ) -> Self {
87 Self {
88 spend,
89 ..Self::new(origin)
90 }
91 }
92
93 pub fn spend(&self) -> &Arc<crate::interface::budget_policy::SpendNode> {
94 &self.spend
95 }
96
97 pub fn set_structured_output(&self, value: Value) {
98 *self
99 .structured_output
100 .lock()
101 .unwrap_or_else(|e| e.into_inner()) = Some(value);
102 }
103
104 pub fn take_structured_output(&self) -> Option<Value> {
105 self.structured_output
106 .lock()
107 .unwrap_or_else(|e| e.into_inner())
108 .take()
109 }
110}
111
112impl ToolContext {
113 pub fn new(
114 cwd: PathBuf,
115 cancel: CancellationToken,
116 exec: crate::interface::exec::ExecProviders,
117 config: Arc<crate::context::EngineConfig>,
118 session: Arc<dyn crate::context::Session>,
119 ) -> Self {
120 // Without the session's TMPDIR: the runtime gives every call the
121 // engine git it made, with the TMPDIR, once for the session.
122 let engine_git = Arc::new(config.sandbox.engine_git(&exec, config.project_trust, None));
123 Self {
124 cwd,
125 session_id: String::new(),
126 turn_no: 0,
127 cancel,
128 config,
129 session,
130 tool_use_id: String::new(),
131 agent_depth: 0,
132 elicitation: None,
133 host_calls: None,
134 exec,
135 dispatch: None,
136 from_code: false,
137 via: "model".into(),
138 confinement: Default::default(),
139 engine_git,
140 attendance: crate::interface::permission::Attendance::Attended,
141 approval: None,
142 turn: Arc::default(),
143 inline_commands: None,
144 path_denials: None,
145 }
146 }
147
148 /// This context's own sandbox settings, to change for it alone: the
149 /// session's stay as they are.
150 pub fn sandbox_mut(&mut self) -> &mut crate::interface::exec::SandboxSettings {
151 Arc::make_mut(&mut Arc::make_mut(&mut self.config).sandbox)
152 }
153
154 /// Whether the call is a delegate's — a sub-agent's or a team member's —
155 /// rather than the conversation's own.
156 pub fn in_delegate(&self) -> bool {
157 self.agent_depth > 0
158 }
159}