1use super::SessionOptions;
8use crate::prompts::{PlanningMode, SystemPromptSlots};
9use crate::queue::SessionQueueConfig;
10use crate::subagent::WorkerAgentSpec;
11use a3s_memory::MemoryStore;
12use std::path::PathBuf;
13use std::sync::Arc;
14
15impl std::fmt::Debug for SessionOptions {
16 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17 f.debug_struct("SessionOptions")
18 .field("model", &self.model)
19 .field("agent_dirs", &self.agent_dirs)
20 .field("worker_agents", &self.worker_agents.len())
21 .field("skill_dirs", &self.skill_dirs)
22 .field("queue_config", &self.queue_config)
23 .field("security_provider", &self.security_provider.is_some())
24 .field("llm_client", &self.llm_client.is_some())
25 .field("context_providers", &self.context_providers.len())
26 .field("confirmation_manager", &self.confirmation_manager.is_some())
27 .field("permission_checker", &self.permission_checker.is_some())
28 .field("permission_policy", &self.permission_policy.is_some())
29 .field("planning_mode", &self.planning_mode)
30 .field("goal_tracking", &self.goal_tracking)
31 .field(
32 "skill_registry",
33 &self
34 .skill_registry
35 .as_ref()
36 .map(|r| format!("{} skills", r.len())),
37 )
38 .field(
39 "enforce_active_skill_tool_restrictions",
40 &self.enforce_active_skill_tool_restrictions,
41 )
42 .field("memory_store", &self.memory_store.is_some())
43 .field("session_store", &self.session_store.is_some())
44 .field("session_id", &self.session_id)
45 .field("auto_save", &self.auto_save)
46 .field("artifact_store_limits", &self.artifact_store_limits)
47 .field("max_parse_retries", &self.max_parse_retries)
48 .field("tool_timeout_ms", &self.tool_timeout_ms)
49 .field("circuit_breaker_threshold", &self.circuit_breaker_threshold)
50 .field("sandbox_handle", &self.sandbox_handle.is_some())
51 .field("workspace_services", &self.workspace_services.is_some())
52 .field("auto_compact", &self.auto_compact)
53 .field("auto_compact_threshold", &self.auto_compact_threshold)
54 .field("continuation_enabled", &self.continuation_enabled)
55 .field("max_continuation_turns", &self.max_continuation_turns)
56 .field("mcp_manager", &self.mcp_manager.is_some())
57 .field("temperature", &self.temperature)
58 .field("thinking_budget", &self.thinking_budget)
59 .field("max_tool_rounds", &self.max_tool_rounds)
60 .field("max_parallel_tasks", &self.max_parallel_tasks)
61 .field("auto_delegation", &self.auto_delegation)
62 .field("auto_parallel_delegation", &self.auto_parallel_delegation)
63 .field("prompt_slots", &self.prompt_slots.is_some())
64 .finish()
65 }
66}
67
68impl SessionOptions {
69 pub fn new() -> Self {
70 Self::default()
71 }
72
73 pub fn with_model(mut self, model: impl Into<String>) -> Self {
74 self.model = Some(model.into());
75 self
76 }
77
78 pub fn with_agent_dir(mut self, dir: impl Into<PathBuf>) -> Self {
79 self.agent_dirs.push(dir.into());
80 self
81 }
82
83 pub fn with_worker_agent(mut self, spec: WorkerAgentSpec) -> Self {
85 self.worker_agents.push(spec);
86 self
87 }
88
89 pub fn with_worker_agents<I>(mut self, specs: I) -> Self
91 where
92 I: IntoIterator<Item = WorkerAgentSpec>,
93 {
94 self.worker_agents.extend(specs);
95 self
96 }
97
98 pub fn with_queue_config(mut self, config: SessionQueueConfig) -> Self {
99 self.queue_config = Some(config);
100 self
101 }
102
103 pub fn with_default_security(mut self) -> Self {
105 self.security_provider = Some(Arc::new(crate::security::DefaultSecurityProvider::new()));
106 self
107 }
108
109 pub fn with_security_provider(
111 mut self,
112 provider: Arc<dyn crate::security::SecurityProvider>,
113 ) -> Self {
114 self.security_provider = Some(provider);
115 self
116 }
117
118 pub fn with_llm_client(mut self, client: Arc<dyn crate::llm::LlmClient>) -> Self {
126 self.llm_client = Some(client);
127 self
128 }
129
130 pub fn with_fs_context(mut self, root_path: impl Into<PathBuf>) -> Self {
132 let config = crate::context::FileSystemContextConfig::new(root_path);
133 self.context_providers
134 .push(Arc::new(crate::context::FileSystemContextProvider::new(
135 config,
136 )));
137 self
138 }
139
140 pub fn with_context_provider(
142 mut self,
143 provider: Arc<dyn crate::context::ContextProvider>,
144 ) -> Self {
145 self.context_providers.push(provider);
146 self
147 }
148
149 pub fn with_confirmation_manager(
151 mut self,
152 manager: Arc<dyn crate::hitl::ConfirmationProvider>,
153 ) -> Self {
154 self.confirmation_manager = Some(manager);
155 self
156 }
157
158 pub fn with_confirmation_policy(mut self, policy: crate::hitl::ConfirmationPolicy) -> Self {
163 self.confirmation_policy = Some(policy);
164 self
165 }
166
167 pub fn with_permission_policy(mut self, policy: crate::permissions::PermissionPolicy) -> Self {
169 self.permission_checker = Some(Arc::new(policy.clone()));
170 self.permission_policy = Some(policy);
171 self
172 }
173
174 pub fn with_permission_checker(
176 mut self,
177 checker: Arc<dyn crate::permissions::PermissionChecker>,
178 ) -> Self {
179 self.permission_checker = Some(checker);
180 self
181 }
182
183 pub fn with_planning_mode(mut self, mode: PlanningMode) -> Self {
185 self.planning_mode = mode;
186 self
187 }
188
189 pub fn with_planning(mut self, enabled: bool) -> Self {
191 self.planning_mode = if enabled {
192 PlanningMode::Enabled
193 } else {
194 PlanningMode::Disabled
195 };
196 self
197 }
198
199 pub fn with_goal_tracking(mut self, enabled: bool) -> Self {
201 self.goal_tracking = enabled;
202 self
203 }
204
205 pub fn with_builtin_skills(mut self) -> Self {
207 self.skill_registry = Some(Arc::new(crate::skills::SkillRegistry::with_builtins()));
208 self
209 }
210
211 pub fn with_skill_registry(mut self, registry: Arc<crate::skills::SkillRegistry>) -> Self {
213 self.skill_registry = Some(registry);
214 self
215 }
216
217 pub fn with_active_skill_tool_restrictions(mut self, enabled: bool) -> Self {
222 self.enforce_active_skill_tool_restrictions = Some(enabled);
223 self
224 }
225
226 pub fn with_skill_dirs(mut self, dirs: impl IntoIterator<Item = impl Into<PathBuf>>) -> Self {
229 self.skill_dirs.extend(dirs.into_iter().map(Into::into));
230 self
231 }
232
233 pub fn with_skills_from_dir(mut self, dir: impl AsRef<std::path::Path>) -> Self {
235 let registry = self
236 .skill_registry
237 .unwrap_or_else(|| Arc::new(crate::skills::SkillRegistry::new()));
238 if let Err(e) = registry.load_from_dir(&dir) {
239 tracing::warn!(
240 dir = %dir.as_ref().display(),
241 error = %e,
242 "Failed to load skills from directory — continuing without them"
243 );
244 }
245 self.skill_registry = Some(registry);
246 self
247 }
248
249 pub fn with_memory(mut self, store: Arc<dyn MemoryStore>) -> Self {
251 self.memory_store = Some(store);
252 self
253 }
254
255 pub fn with_file_memory(mut self, dir: impl Into<PathBuf>) -> Self {
261 self.file_memory_dir = Some(dir.into());
262 self
263 }
264
265 pub fn with_session_store(mut self, store: Arc<dyn crate::store::SessionStore>) -> Self {
267 self.session_store = Some(store);
268 self
269 }
270
271 pub fn with_file_session_store(mut self, dir: impl Into<PathBuf>) -> Self {
273 let dir = dir.into();
274 match tokio::runtime::Handle::try_current() {
275 Ok(handle) => {
276 match tokio::task::block_in_place(|| {
277 handle.block_on(crate::store::FileSessionStore::new(dir))
278 }) {
279 Ok(store) => {
280 self.session_store =
281 Some(Arc::new(store) as Arc<dyn crate::store::SessionStore>);
282 }
283 Err(e) => {
284 tracing::warn!("Failed to create file session store: {}", e);
285 }
286 }
287 }
288 Err(_) => {
289 tracing::warn!(
290 "No async runtime available for file session store — persistence disabled"
291 );
292 }
293 }
294 self
295 }
296
297 pub fn with_session_id(mut self, id: impl Into<String>) -> Self {
299 self.session_id = Some(id.into());
300 self
301 }
302
303 pub fn with_tenant_id(mut self, tenant: impl Into<String>) -> Self {
306 self.tenant_id = Some(tenant.into());
307 self
308 }
309
310 pub fn with_principal(mut self, principal: impl Into<String>) -> Self {
313 self.principal = Some(principal.into());
314 self
315 }
316
317 pub fn with_agent_template_id(mut self, template_id: impl Into<String>) -> Self {
320 self.agent_template_id = Some(template_id.into());
321 self
322 }
323
324 pub fn with_correlation_id(mut self, corr: impl Into<String>) -> Self {
327 self.correlation_id = Some(corr.into());
328 self
329 }
330
331 pub fn with_budget_guard(mut self, guard: Arc<dyn crate::budget::BudgetGuard>) -> Self {
336 self.budget_guard = Some(guard);
337 self
338 }
339
340 pub fn with_host_env(mut self, env: Arc<crate::host_env::HostEnv>) -> Self {
346 self.host_env = Some(env);
347 self
348 }
349
350 pub fn with_retention_limits(
360 mut self,
361 limits: crate::retention::SessionRetentionLimits,
362 ) -> Self {
363 self.retention_limits = Some(limits);
364 self
365 }
366
367 pub fn with_auto_save(mut self, enabled: bool) -> Self {
369 self.auto_save = enabled;
370 self
371 }
372
373 pub fn with_artifact_store_limits(mut self, limits: crate::tools::ArtifactStoreLimits) -> Self {
375 self.artifact_store_limits = Some(limits);
376 self
377 }
378
379 pub fn with_parse_retries(mut self, max: u32) -> Self {
385 self.max_parse_retries = Some(max);
386 self
387 }
388
389 pub fn with_tool_timeout(mut self, timeout_ms: u64) -> Self {
395 self.tool_timeout_ms = Some(timeout_ms);
396 self
397 }
398
399 pub fn with_circuit_breaker(mut self, threshold: u32) -> Self {
405 self.circuit_breaker_threshold = Some(threshold);
406 self
407 }
408
409 pub fn with_resilience_defaults(self) -> Self {
415 self.with_parse_retries(2)
416 .with_tool_timeout(120_000)
417 .with_circuit_breaker(3)
418 }
419
420 pub fn with_sandbox_handle(mut self, handle: Arc<dyn crate::sandbox::BashSandbox>) -> Self {
428 self.sandbox_handle = Some(handle);
429 self
430 }
431
432 pub fn with_workspace_backend(
438 mut self,
439 services: Arc<crate::workspace::WorkspaceServices>,
440 ) -> Self {
441 self.workspace_services = Some(services);
442 self
443 }
444
445 pub fn with_auto_compact(mut self, enabled: bool) -> Self {
450 self.auto_compact = enabled;
451 self
452 }
453
454 pub fn with_auto_compact_threshold(mut self, threshold: f32) -> Self {
456 self.auto_compact_threshold = Some(threshold.clamp(0.0, 1.0));
457 self
458 }
459
460 pub fn with_continuation(mut self, enabled: bool) -> Self {
465 self.continuation_enabled = Some(enabled);
466 self
467 }
468
469 pub fn with_max_continuation_turns(mut self, turns: u32) -> Self {
471 self.max_continuation_turns = Some(turns);
472 self
473 }
474
475 pub fn with_mcp(mut self, manager: Arc<crate::mcp::manager::McpManager>) -> Self {
480 self.mcp_manager = Some(manager);
481 self
482 }
483
484 pub fn with_temperature(mut self, temperature: f32) -> Self {
485 self.temperature = Some(temperature);
486 self
487 }
488
489 pub fn with_thinking_budget(mut self, budget: usize) -> Self {
490 self.thinking_budget = Some(budget);
491 self
492 }
493
494 pub fn with_max_tool_rounds(mut self, rounds: usize) -> Self {
499 self.max_tool_rounds = Some(rounds);
500 self
501 }
502
503 pub fn with_max_parallel_tasks(mut self, tasks: usize) -> Self {
505 self.max_parallel_tasks = Some(tasks.max(1));
506 self
507 }
508
509 pub fn with_auto_delegation(mut self, config: crate::config::AutoDelegationConfig) -> Self {
511 self.auto_delegation = Some(config);
512 self
513 }
514
515 pub fn with_auto_delegation_enabled(mut self, enabled: bool) -> Self {
517 let mut config = self.auto_delegation.take().unwrap_or_default();
518 config.enabled = enabled;
519 self.auto_delegation = Some(config);
520 self
521 }
522
523 pub fn with_auto_parallel_delegation(mut self, enabled: bool) -> Self {
527 if let Some(config) = &mut self.auto_delegation {
528 config.auto_parallel = enabled;
529 }
530 self.auto_parallel_delegation = Some(enabled);
531 self
532 }
533
534 pub fn with_prompt_slots(mut self, slots: SystemPromptSlots) -> Self {
539 self.prompt_slots = Some(slots);
540 self
541 }
542
543 pub fn with_hook_executor(mut self, executor: Arc<dyn crate::hooks::HookExecutor>) -> Self {
549 self.hook_executor = Some(executor);
550 self
551 }
552}