Skip to main content

oxi_agent/
config.rs

1/// Agent configuration
2use oxi_ai::CompactionStrategy;
3use serde::{Deserialize, Serialize};
4use std::sync::Arc;
5
6fn default_context_window() -> usize {
7    128_000
8}
9
10/// Hook context for `shouldStopAfterTurn`.
11#[derive(Debug, Clone)]
12pub struct ShouldStopAfterTurnContext {
13    /// The assistant message that completed the turn.
14    pub message: oxi_ai::AssistantMessage,
15    /// Tool result messages from this turn.
16    pub tool_results: Vec<oxi_ai::ToolResultMessage>,
17    /// Current iteration number.
18    pub iteration: usize,
19}
20
21/// Result of `beforeToolCall` hook.
22#[derive(Debug, Clone, Default)]
23pub struct BeforeToolCallResult {
24    /// If `true`, the tool call is blocked and an error result is returned.
25    pub block: bool,
26    /// Human-readable reason for blocking.
27    pub reason: Option<String>,
28}
29
30/// Result of `afterToolCall` hook.
31#[derive(Debug, Clone, Default)]
32pub struct AfterToolCallResult {
33    /// Override content for the tool result.
34    pub content: Option<String>,
35    /// Override error status.
36    pub is_error: Option<bool>,
37    /// Signal that the agent should stop after this batch.
38    pub terminate: Option<bool>,
39    /// Arbitrary structured details returned by the hook.
40    ///
41    /// Consumers (e.g. telemetry, middleware) can use this to attach
42    /// extra context without extending the struct.
43    pub details: Option<serde_json::Value>,
44}
45
46/// Hook context for `beforeToolCall`.
47#[derive(Debug, Clone)]
48pub struct BeforeToolCallContext {
49    /// The tool call being made.
50    pub tool_call_id: String,
51    /// Tool name.
52    pub tool_name: String,
53    /// Validated arguments.
54    pub args: serde_json::Value,
55}
56
57/// Hook context for `afterToolCall`.
58#[derive(Debug, Clone)]
59pub struct AfterToolCallContext {
60    /// The tool call that was made.
61    pub tool_call_id: String,
62    /// Tool name.
63    pub tool_name: String,
64    /// The tool result content.
65    pub result: String,
66    /// Whether the result is an error.
67    pub is_error: bool,
68    /// Arbitrary structured details provided to the hook.
69    ///
70    /// Set by the agent loop before invoking the hook so that consumers
71    /// receive extra context (e.g. execution timing, tool-specific metadata).
72    pub details: Option<serde_json::Value>,
73}
74
75/// Callback hooks for the agent loop.
76///
77/// These mirror pi-mono's `AgentLoopConfig` hooks, allowing callers to
78/// inject custom logic at key points in the agentic loop.
79#[derive(Default)]
80#[allow(clippy::type_complexity)]
81pub struct AgentHooks {
82    /// Called after each turn completes. Return `true` to stop the agent loop.
83    ///
84    /// Wrapped in `Arc` so the hook can be invoked multiple times without
85    /// being consumed (unlike `Box<dyn Fn>` which requires `take()`).
86    pub should_stop_after_turn:
87        Option<Arc<dyn Fn(&ShouldStopAfterTurnContext) -> bool + Send + Sync>>,
88
89    /// Called before a tool is executed. Return a `BeforeToolCallResult` with
90    /// `block: true` to prevent execution.
91    #[allow(clippy::type_complexity)]
92    pub before_tool_call:
93        Option<Box<dyn Fn(&BeforeToolCallContext) -> BeforeToolCallResult + Send + Sync>>,
94
95    /// Called after a tool execution completes. Can override the result.
96    #[allow(clippy::type_complexity)]
97    pub after_tool_call:
98        Option<Box<dyn Fn(&AfterToolCallContext) -> AfterToolCallResult + Send + Sync>>,
99
100    /// Returns steering messages to inject mid-run. Called after each turn
101    /// (unless stopped).
102    #[allow(clippy::type_complexity)]
103    pub get_steering_messages: Option<Arc<dyn Fn() -> Vec<String> + Send + Sync>>,
104
105    /// Returns follow-up messages to process after the agent would stop.
106    /// Called when the agent has no more tool calls and no steering messages.
107    #[allow(clippy::type_complexity)]
108    pub get_follow_up_messages: Option<Arc<dyn Fn() -> Vec<String> + Send + Sync>>,
109
110    /// Tool execution mode.
111    pub tool_execution: ToolExecutionMode,
112}
113
114/// How tool calls are executed within a single assistant turn.
115#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
116pub enum ToolExecutionMode {
117    /// Execute tool calls sequentially, one at a time.
118    Sequential,
119    /// Execute tool calls concurrently (in parallel).
120    #[default]
121    Parallel,
122}
123
124/// Agent runtime configuration
125#[derive(Debug, Clone, Serialize, Deserialize)]
126pub struct AgentConfig {
127    /// Agent name
128    pub name: String,
129    /// Agent description
130    pub description: Option<String>,
131    /// Model ID to use
132    pub model_id: String,
133    /// System prompt
134    pub system_prompt: Option<String>,
135    /// Timeout in seconds for the entire agent run
136    pub timeout_seconds: u64,
137    /// Temperature for generation (0.0 to 1.0)
138    pub temperature: Option<f64>,
139    /// Maximum tokens to generate
140    pub max_tokens: Option<usize>,
141    /// Compaction strategy for long conversations
142    #[serde(default)]
143    pub compaction_strategy: CompactionStrategy,
144    /// Custom instruction passed to the compactor
145    #[serde(default)]
146    pub compaction_instruction: Option<String>,
147    /// Model context window size (used for threshold-based compaction)
148    #[serde(default = "default_context_window")]
149    pub context_window: usize,
150    /// API key override for the provider.
151    ///
152    /// When set, this is injected into [`oxi_ai::StreamOptions`] so the
153    /// provider uses it instead of an environment variable.
154    #[serde(default)]
155    pub api_key: Option<String>,
156    /// Working directory for file tools. Defaults to current directory if None.
157    #[serde(default)]
158    pub workspace_dir: Option<std::path::PathBuf>,
159    /// Output mode for agent responses.
160    ///
161    /// When set, the agent extracts structured output from the final response.
162    /// See [`OutputMode`] for available modes.
163    ///
164    /// [`OutputMode`]: crate::structured_output::OutputMode
165    #[serde(default)]
166    pub output_mode: Option<String>,
167    /// Session identity used by tools that gate behavior on liveness (e.g. the
168    /// `issue` tool's `start`/`close` ownership checks). When `Some`, this value
169    /// is threaded through to [`crate::tools::ToolContext::session_id`].
170    /// `None` means the tool receives `session_id == None` and ownership-gated
171    /// operations will reject the call (defensive default).
172    #[serde(default)]
173    pub session_id: Option<String>,
174
175    /// Per-provider options for fine-grained control.
176    ///
177    /// When set, these are passed through to [`oxi_ai::StreamOptions::provider_options`]
178    /// so the provider can read provider-specific settings (e.g. Anthropic adaptive
179    /// thinking, OpenAI reasoning_effort, Google thinkingConfig).
180    #[serde(default)]
181    pub provider_options: Option<oxi_ai::ProviderOptions>,
182
183    /// TTSR engine for stream rule checking. When set, streaming output
184    /// is checked against registered rules and violations trigger
185    /// [`crate::agent_loop::StreamOutcome::RuleInterrupt`].
186    #[serde(skip, default)]
187    pub ttsr_engine: Option<std::sync::Arc<crate::agent_loop::ttsr::TtsrEngine>>,
188
189    /// Memory backend for `memory_*` tools.
190    #[serde(skip, default)]
191    pub memory: Option<std::sync::Arc<dyn crate::tools::MemoryBackend>>,
192    /// Todo state provider for the `todo` tool.
193    #[serde(skip, default)]
194    pub todo: Option<std::sync::Arc<dyn crate::tools::TodoStateProvider>>,
195    /// Agent pool for Hub display and sub-agent matching.
196    #[serde(skip, default)]
197    pub agent_pool: Option<std::sync::Arc<dyn crate::tools::AgentPoolProvider>>,
198}
199
200impl Default for AgentConfig {
201    fn default() -> Self {
202        Self {
203            name: "oxi-agent".to_string(),
204            description: None,
205            model_id: "claude-sonnet-4-20250514".to_string(),
206            system_prompt: None,
207            timeout_seconds: 300,
208            temperature: None,
209            max_tokens: None,
210            compaction_strategy: CompactionStrategy::default(),
211            compaction_instruction: None,
212            context_window: 128_000,
213            api_key: None,
214            workspace_dir: None,
215            output_mode: None,
216            provider_options: None,
217            session_id: None,
218            ttsr_engine: None,
219            memory: None,
220            todo: None,
221            agent_pool: None,
222        }
223    }
224}
225
226impl AgentConfig {
227    /// Create a new config with the given model ID.
228    pub fn new(model_id: impl Into<String>) -> Self {
229        Self {
230            model_id: model_id.into(),
231            ..Default::default()
232        }
233    }
234
235    /// Set the agent name.
236    pub fn with_name(mut self, name: impl Into<String>) -> Self {
237        self.name = name.into();
238        self
239    }
240
241    /// Set the system prompt.
242    pub fn with_system_prompt(mut self, prompt: impl Into<String>) -> Self {
243        self.system_prompt = Some(prompt.into());
244        self
245    }
246
247    /// Set the timeout in seconds for the entire agent run.
248    pub fn with_timeout(mut self, seconds: u64) -> Self {
249        self.timeout_seconds = seconds;
250        self
251    }
252
253    /// Set the compaction strategy for long conversations.
254    pub fn with_compaction_strategy(mut self, strategy: CompactionStrategy) -> Self {
255        self.compaction_strategy = strategy;
256        self
257    }
258
259    /// Set a custom instruction passed to the compactor.
260    pub fn with_compaction_instruction(mut self, instruction: impl Into<String>) -> Self {
261        self.compaction_instruction = Some(instruction.into());
262        self
263    }
264
265    /// Set the session identity threaded into [`crate::tools::ToolContext::session_id`].
266    ///
267    /// Tools that gate behavior on liveness (e.g. an `issue` tool's
268    /// `start`/`close` ownership checks) use this to identify the caller.
269    /// Leaving it `None` causes those tools to see an empty caller id and
270    /// reject ownership-gated operations (defensive default).
271    pub fn with_session_id(mut self, session_id: impl Into<String>) -> Self {
272        self.session_id = Some(session_id.into());
273        self
274    }
275}
276
277#[cfg(test)]
278mod tests {
279    use super::*;
280
281    #[test]
282    fn session_id_defaults_to_none() {
283        let c = AgentConfig::default();
284        assert!(c.session_id.is_none(), "default session_id must be None");
285    }
286
287    #[test]
288    fn with_session_id_sets_the_field() {
289        let c = AgentConfig::new("m").with_session_id("proc-42");
290        assert_eq!(c.session_id.as_deref(), Some("proc-42"));
291    }
292
293    #[test]
294    fn session_id_round_trips_through_serde() {
295        // Forward-compat: a serialized config with the new field deserializes back.
296        let with = AgentConfig::new("m").with_session_id("proc-7");
297        let json = serde_json::to_string(&with).unwrap();
298        assert!(json.contains("\"session_id\":"));
299        let back: AgentConfig = serde_json::from_str(&json).unwrap();
300        assert_eq!(back.session_id.as_deref(), Some("proc-7"));
301
302        // Backward-compat: a payload WITHOUT the session_id key must still
303        // deserialize and default the field to None. We build that payload by
304        // serializing a config, then stripping the key with serde_json::Value.
305        let mut v: serde_json::Value =
306            serde_json::from_str(&json).expect("config serializes to valid JSON");
307        if let Some(obj) = v.as_object_mut() {
308            obj.remove("session_id");
309        }
310        let stripped = serde_json::to_string(&v).unwrap();
311        let legacy: AgentConfig = serde_json::from_str(&stripped).unwrap();
312        assert!(
313            legacy.session_id.is_none(),
314            "payload missing session_id must default to None"
315        );
316    }
317}