Skip to main content

phi_core/agent_loop/
config.rs

1use crate::context::{ContextConfig, ExecutionLimits};
2use crate::provider::context_translation::ContextTranslationStrategy;
3use crate::provider::{ModelConfig, ResponseFormat, StreamProvider};
4use crate::types::*;
5use std::future::Future;
6use std::pin::Pin;
7use std::sync::Arc;
8
9// ── Context transformation callbacks ────────────────────────────────────────
10/// All hook types use `Arc` (shared ownership) so they can be cloned into closures
11/// and stored without lifetime complications. `Box<dyn Fn>` would suffice for single-owner
12/// cases but `Arc` makes it trivially cheap to share across async tasks.
13/// Converts `AgentMessage[]` → `Message[]` before each LLM call.
14pub type ConvertToLlmFn = Arc<dyn Fn(&[AgentMessage]) -> Vec<Message> + Send + Sync>;
15/// Transforms the full context before `convert_to_llm` (for pruning, reordering, injection).
16pub type TransformContextFn = Arc<dyn Fn(Vec<AgentMessage>) -> Vec<AgentMessage> + Send + Sync>;
17/// Returns pending messages (steering interrupts or follow-up work) when polled.
18pub type GetMessagesFn = Box<dyn Fn() -> Vec<AgentMessage> + Send + Sync>;
19
20// ── 0.9.0 async lifecycle hooks ─────────────────────────────────────────────
21//
22// All lifecycle Fn types below are async-trait-style boxed futures. To
23// construct one from a sync closure body:
24//
25// ```rust,ignore
26// let hook: BeforeTurnFn = Arc::new(|messages, turn| {
27//     Box::pin(async move {
28//         // sync logic ...
29//         true
30//     })
31// });
32// ```
33//
34// For async closure bodies, simply `.await` inside the `async move` block.
35
36/// Boxed-future return type used by all 0.9.0 async lifecycle hooks. `T` is the
37/// hook's logical return value (often `bool` for veto-returning hooks or `()`
38/// for fire-and-forget hooks).
39pub type HookFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
40
41// ── Loop hooks ───────────────────────────────────────────────────────────────
42/// Called once before the entire agent loop begins (before `AgentStart` is emitted).
43///
44/// Arguments: `(messages, loop_index)` — `messages` is the full context at the time of the call;
45/// `loop_index` is always `0` (reserved for future multi-loop scenarios).
46/// Return `false` to abort: `AgentEnd` is emitted immediately with an empty message list.
47///
48/// 0.9.0: async hook. Wrap sync bodies in `Box::pin(async move { ... })`.
49pub type BeforeLoopFn =
50    Arc<dyn for<'a> Fn(&'a [AgentMessage], usize) -> HookFuture<'a, bool> + Send + Sync>;
51/// Called once after the entire agent loop ends (after `AgentEnd` is emitted).
52///
53/// Arguments: `(new_messages, accumulated_usage)` — `new_messages` are the messages produced
54/// by this loop call; `accumulated_usage` sums input/output tokens across all turns.
55///
56/// 0.9.0: async hook.
57pub type AfterLoopFn =
58    Arc<dyn for<'a> Fn(&'a [AgentMessage], &'a Usage) -> HookFuture<'a, ()> + Send + Sync>;
59
60// ── Turn hooks ───────────────────────────────────────────────────────────────
61/// Called before each LLM turn (before `TurnStart` is emitted).
62///
63/// Arguments: `(messages, turn_index)` — `messages` is the full context (steering messages
64/// queued for *this* turn are not yet visible); `turn_index` is 0-based.
65/// Return `false` to abort the turn: no `TurnStart`/`TurnEnd` events are emitted,
66/// but `AgentEnd` still fires normally.
67///
68/// 0.9.0: async hook.
69pub type BeforeTurnFn =
70    Arc<dyn for<'a> Fn(&'a [AgentMessage], usize) -> HookFuture<'a, bool> + Send + Sync>;
71/// Called after each LLM turn (after `TurnEnd` is emitted).
72///
73/// Arguments: `(messages, turn_usage)` — `turn_usage` covers only this turn's tokens.
74/// Fires on both the normal path and the error/abort path.
75///
76/// 0.9.0: async hook.
77pub type AfterTurnFn =
78    Arc<dyn for<'a> Fn(&'a [AgentMessage], &'a Usage) -> HookFuture<'a, ()> + Send + Sync>;
79
80// ── Tool execution hooks ─────────────────────────────────────────────────────
81/// Called before each tool call (before `ToolExecutionStart` is emitted).
82///
83/// Arguments: `(tool_name, tool_call_id, args)`.
84/// Return `false` to skip the call: an error `ToolResult` is synthesised so the LLM still
85/// receives a response, but `ToolExecutionStart`/`End` are **not** emitted.
86///
87/// 0.9.0: async hook.
88pub type BeforeToolExecutionFn = Arc<
89    dyn for<'a> Fn(&'a str, &'a str, &'a serde_json::Value) -> HookFuture<'a, bool> + Send + Sync,
90>;
91/// Called after each tool call (after `ToolExecutionEnd` is emitted).
92///
93/// Arguments: `(tool_name, tool_call_id, is_error)`.
94///
95/// 0.9.0: async hook.
96pub type AfterToolExecutionFn =
97    Arc<dyn for<'a> Fn(&'a str, &'a str, bool) -> HookFuture<'a, ()> + Send + Sync>;
98/// Called before each incremental tool update (before `ToolExecutionUpdate` is emitted).
99///
100/// Fires every time a tool calls `ctx.on_update(partial)` — potentially many times per call
101/// (e.g. each line of bash output). Arguments: `(tool_name, tool_call_id, text_content)`.
102/// Return `false` to suppress the streaming event; the tool keeps running and its final
103/// `ToolResult` (what the LLM sees) is **unaffected**.
104///
105/// **Sync** (not async). Tool-update hooks fire from inside the sync
106/// `ToolUpdateFn` callback that tools invoke during their async execute
107/// body — making this hook async would require also async-ifying
108/// `ToolUpdateFn` and every tool that invokes `ctx.on_update(...)`. The
109/// veto decision must be synchronous so the surrounding emit gate works
110/// without blocking. For async work in update-time, dispatch via
111/// `tokio::spawn` inside the closure body.
112pub type BeforeToolExecutionUpdateFn = Arc<dyn Fn(&str, &str, &str) -> bool + Send + Sync>;
113/// Called after each incremental tool update (after `ToolExecutionUpdate` is emitted).
114///
115/// Only fires when the update was *not* suppressed by `BeforeToolExecutionUpdateFn`.
116/// Arguments: `(tool_name, tool_call_id, text_content)`.
117///
118/// **Sync** (not async). See [`BeforeToolExecutionUpdateFn`] for the
119/// rationale.
120pub type AfterToolExecutionUpdateFn = Arc<dyn Fn(&str, &str, &str) + Send + Sync>;
121
122/// Called when the LLM returns `StopReason::Error`. Argument: the error message string.
123///
124/// 0.9.0: async hook.
125pub type OnErrorFn = Arc<dyn for<'a> Fn(&'a str) -> HookFuture<'a, ()> + Send + Sync>;
126
127// ── Compaction hooks (G1) ───────────────────────────────────────────────────
128/// Called before compaction starts.
129///
130/// Arguments: `(estimated_tokens, message_count)`.
131/// Return `false` to skip compaction for this cycle.
132///
133/// 0.9.0: async hook.
134pub type BeforeCompactionStartFn =
135    Arc<dyn Fn(usize, usize) -> HookFuture<'static, bool> + Send + Sync>;
136/// Called after compaction completes.
137///
138/// Arguments: `(messages_before, messages_after, tokens_before, tokens_after)`.
139///
140/// 0.9.0: async hook.
141pub type AfterCompactionEndFn =
142    Arc<dyn Fn(usize, usize, usize, usize) -> HookFuture<'static, ()> + Send + Sync>;
143
144/// All static settings for a single [`agent_loop`] / [`agent_loop_continue`] call.
145///
146/// Build with the public fields directly or via [`crate::agent::Agent`]'s builder methods.
147/// The config is borrowed (`&AgentLoopConfig`) throughout the loop — it is never mutated.
148///
149/// ## Lifecycle hooks
150///
151/// All hook fields are `Option<Arc<dyn Fn(...)>>`. `None` means "no hook" (zero overhead).
152/// See the module-level doc for the guaranteed ordering relative to [`AgentEvent`]s.
153pub struct AgentLoopConfig {
154    /// Complete provider identity: model id, api_key, base_url, protocol, compat flags, cost rates.
155    /// The agent loop resolves the concrete `StreamProvider` from `model_config.api` via
156    /// `ProviderRegistry`. Set `provider_override` to bypass the registry for custom providers.
157    pub model_config: ModelConfig,
158
159    /// Custom provider override. When `Some`, bypasses `ProviderRegistry` dispatch and uses
160    /// this provider directly. Useful for testing (`MockProvider`) or custom implementations.
161    /// When `None` (the default), the provider is resolved from `model_config.api`.
162    pub provider_override: Option<Arc<dyn StreamProvider>>,
163
164    pub thinking_level: ThinkingLevel,
165    pub max_tokens: Option<u32>,
166    pub temperature: Option<f32>,
167
168    /// Convert AgentMessage[] → Message[] before each LLM call.
169    /// Default: keep only LLM-compatible messages.
170    pub convert_to_llm: Option<ConvertToLlmFn>,
171
172    /// Transform context before convert_to_llm (for pruning, compaction).
173    pub transform_context: Option<TransformContextFn>,
174
175    /// Get steering messages (user interruptions mid-run).
176    pub get_steering_messages: Option<GetMessagesFn>,
177
178    /// Get follow-up messages (queued work after agent finishes).
179    pub get_follow_up_messages: Option<GetMessagesFn>,
180
181    /// Context window configuration (auto-compaction).
182    /// Compaction strategies are now part of `ContextConfig.compaction` (G5 consolidation).
183    pub context_config: Option<ContextConfig>,
184
185    /// Execution limits (max turns, tokens, duration, cost).
186    /// Cost is tracked automatically using `model_config.cost` rates after each turn.
187    /// `ExecutionLimits.max_cost` enforcement is active whenever rates are non-zero.
188    pub execution_limits: Option<ExecutionLimits>,
189
190    /// Prompt caching configuration.
191    pub cache_config: CacheConfig, //from types.rs
192
193    /// Tool execution strategy (sequential, parallel, or batched).
194    pub tool_execution: ToolExecutionStrategy, // from types.rs
195
196    /// Per-tool execution timeout.
197    ///
198    /// When `Some(d)`, each individual `AgentTool::execute()` call is bounded by `d`.
199    /// On expiry, the tool's child cancel token is signalled (cooperative cleanup) and a
200    /// `ToolError::Timeout` is synthesised as the tool result — the LLM sees the failure
201    /// and the agent loop continues. A per-tool override via `AgentTool::timeout()` takes
202    /// precedence over this field. `None` (the default) means no per-tool timeout.
203    pub tool_timeout: Option<std::time::Duration>,
204
205    /// Retry configuration for transient provider errors.
206    pub retry_config: crate::provider::retry::RetryConfig,
207
208    //******* Callbacks Turn *******
209    /// Called before each LLM turn. Return `false` to abort the turn.
210    pub before_turn: Option<BeforeTurnFn>,
211    /// Called after each LLM turn with the current messages and the turn's usage.
212    pub after_turn: Option<AfterTurnFn>,
213
214    //******* Callbacks Loop *******
215    /// Called before each Agent loop. Return `false` to abort the loop.
216    pub before_loop: Option<BeforeLoopFn>,
217    /// Called after each Agent loop with the current messages and the loop's usage.
218    pub after_loop: Option<AfterLoopFn>,
219
220    //******* Callbacks Tool Execution *******
221    /// Called before each tool execution. Return `false` to skip the tool call.
222    pub before_tool_execution: Option<BeforeToolExecutionFn>,
223    /// Called after each tool execution.
224    pub after_tool_execution: Option<AfterToolExecutionFn>,
225    /// Called before each ToolExecutionUpdate event. Return `false` to suppress the event.
226    pub before_tool_execution_update: Option<BeforeToolExecutionUpdateFn>,
227    /// Called after each ToolExecutionUpdate event.
228    pub after_tool_execution_update: Option<AfterToolExecutionUpdateFn>,
229
230    /// Called when the LLM returns a `StopReason::Error`.
231    pub on_error: Option<OnErrorFn>,
232
233    //******* Callbacks Compaction (G1) *******
234    /// Called before compaction starts. Return `false` to skip compaction.
235    pub before_compaction_start: Option<BeforeCompactionStartFn>,
236    /// Called after compaction completes.
237    pub after_compaction_end: Option<AfterCompactionEndFn>,
238
239    /// Input filters applied to user messages before the LLM call.
240    /// Filters run in order; first `Reject` wins and discards any accumulated
241    /// warnings. `Warn` messages accumulate and are appended to the user message.
242    pub input_filters: Vec<Arc<dyn InputFilter>>, // from types.rs
243
244    /// The trigger type for the first TurnStart event in this run.
245    /// Defaults to `TurnTrigger::User`; set to `SubAgent` by sub-agent callers.
246    pub first_turn_trigger: TurnTrigger,
247
248    /// Stable identity for this config, used as the middle segment of `loop_id`:
249    ///   `loop_id = "{session_id}.{config_id}.{N}"`
250    ///
251    /// When `None` and the `Agent` wrapper is used, the identity is auto-derived by
252    /// `Agent::next_loop_id()` from the provider, model, and thinking level:
253    ///   `"{provider_id}.{model_slug}[.thinking]"`
254    ///
255    /// For direct callers of `agent_loop`, set `context.loop_id` explicitly — this field
256    /// is only read by `Agent::next_loop_id()` and has no effect inside `agent_loop` itself.
257    ///
258    /// Set explicitly for human-readable or deterministic loop IDs, e.g.:
259    ///   `config.config_id = Some("experiment-A".to_string());`
260    ///   → loop IDs: `ses_xyz.experiment-A.1`, `ses_xyz.experiment-A.2`, …
261    pub config_id: Option<String>,
262
263    /// G8 — Optional context translation strategy for cross-provider compatibility.
264    ///
265    /// When set, messages are translated through this strategy before being sent to
266    /// the LLM provider. This allows content types from one provider (e.g.,
267    /// `Content::Thinking` from Anthropic) to be translated or removed when targeting
268    /// a different provider. The translation is read-only — originals are never modified.
269    pub context_translation: Option<Arc<dyn ContextTranslationStrategy>>,
270
271    /// Shared state for PrunTool to communicate pruning requests to the loop.
272    pub prun_pending: Option<Arc<std::sync::Mutex<Vec<crate::tools::prun::PrunRequest>>>>,
273
274    /// Shared state for [`RevertTool`](crate::tools::RevertTool) to communicate
275    /// revert requests to the loop. `Some` iff the agent was constructed via
276    /// [`BasicAgent::with_revert_tool`](crate::agents::BasicAgent::with_revert_tool);
277    /// `apply_revert` (Phase 3) is gated on this being `Some`, so the LLM has
278    /// no path to invoke the tool when the builder did not opt in.
279    pub revert_pending: Option<Arc<std::sync::Mutex<Vec<crate::tools::revert::RevertRequest>>>>,
280
281    /// Desired LLM output shape. Default `Text` preserves the historical free-form
282    /// behaviour; `JsonObject` / `JsonSchema` request constrained structured output
283    /// from providers that support it. See `provider::ResponseFormat` and the
284    /// capability matrix in `docs/specs/developer/provider.md`.
285    pub response_format: ResponseFormat,
286}