Skip to main content

merry_runtime/interactive/
settings.rs

1use crate::{
2    CompactionConfig, PermissionAdmissionSource, PermissionReviewMode, RuntimeModelRole,
3    SubagentConfig,
4};
5use merry_llm::{GenerationConfig, ModelName, ModelProvider, ModelRetryPolicy, ReasoningEffort};
6use std::{
7    num::{NonZeroU64, NonZeroUsize},
8    sync::Arc,
9};
10
11/// Provider-neutral model selection for subsequent interactive requests.
12#[derive(Clone)]
13pub struct InteractivePrimaryModel {
14    pub(crate) provider: Arc<dyn ModelProvider>,
15    pub(crate) model: ModelName,
16    pub(crate) retry_policy: ModelRetryPolicy,
17    pub(crate) reasoning_effort: Option<ReasoningEffort>,
18}
19
20/// Subagent admission and concurrency settings for subsequent delegated work.
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub struct InteractiveSubagentSettings {
23    pub(super) enabled: bool,
24    pub(super) config: SubagentConfig,
25}
26
27impl InteractiveSubagentSettings {
28    #[must_use]
29    pub fn new(enabled: bool, config: SubagentConfig) -> Self {
30        Self { enabled, config }
31    }
32}
33
34impl InteractivePrimaryModel {
35    #[must_use]
36    pub fn new(
37        provider: Arc<dyn ModelProvider>,
38        model: ModelName,
39        retry_policy: ModelRetryPolicy,
40    ) -> Self {
41        Self {
42            provider,
43            model,
44            retry_policy,
45            reasoning_effort: None,
46        }
47    }
48
49    /// Attaches an optional role-specific reasoning effort override.
50    #[must_use]
51    pub fn with_reasoning_effort(mut self, reasoning_effort: Option<ReasoningEffort>) -> Self {
52        self.reasoning_effort = reasoning_effort;
53        self
54    }
55}
56
57/// Runtime settings applied by an interactive run at the next model-request boundary.
58#[derive(Clone, Default)]
59pub struct InteractiveSettingsUpdate {
60    pub(super) generation_config: Option<GenerationConfig>,
61    pub(super) primary_model: Option<InteractivePrimaryModel>,
62    pub(super) role_models: Vec<(RuntimeModelRole, Option<InteractivePrimaryModel>)>,
63    pub(super) subagents: Option<InteractiveSubagentSettings>,
64    pub(super) automatic_compaction: Option<CompactionConfig>,
65    pub(super) context_window_tokens: Option<Option<NonZeroU64>>,
66    pub(super) max_parallel_tool_calls: Option<NonZeroUsize>,
67    pub(super) permission_review_mode: Option<PermissionReviewMode>,
68    pub(super) permission_admission_source: Option<Arc<dyn PermissionAdmissionSource>>,
69}
70
71impl InteractiveSettingsUpdate {
72    /// Replaces the generation configuration used by subsequent model requests.
73    #[must_use]
74    pub fn with_generation_config(mut self, generation_config: GenerationConfig) -> Self {
75        self.generation_config = Some(generation_config);
76        self
77    }
78
79    /// Replaces the primary provider and model used by subsequent model requests.
80    #[must_use]
81    pub fn with_primary_model(mut self, primary_model: InteractivePrimaryModel) -> Self {
82        self.primary_model = Some(primary_model);
83        self
84    }
85
86    /// Replaces or clears a role-specific provider/model/effort override (such as
87    /// `ContextCompaction` or `ApprovalReview`). Passing `None` falls back to primary.
88    #[must_use]
89    pub fn with_role_model(
90        mut self,
91        role: RuntimeModelRole,
92        model: Option<InteractivePrimaryModel>,
93    ) -> Self {
94        self.role_models.push((role, model));
95        self
96    }
97
98    /// Replaces subagent admission and concurrency settings.
99    #[must_use]
100    pub fn with_subagents(mut self, subagents: InteractiveSubagentSettings) -> Self {
101        self.subagents = Some(subagents);
102        self
103    }
104
105    /// Replaces automatic compaction policy for subsequent model requests.
106    #[must_use]
107    pub fn with_automatic_compaction(mut self, automatic_compaction: CompactionConfig) -> Self {
108        self.automatic_compaction = Some(automatic_compaction);
109        self
110    }
111
112    /// Replaces or clears the explicit context-window override for subsequent requests.
113    #[must_use]
114    pub fn with_context_window_tokens(mut self, context_window_tokens: Option<NonZeroU64>) -> Self {
115        self.context_window_tokens = Some(context_window_tokens);
116        self
117    }
118
119    /// Replaces the maximum concurrent parallel tool calls per tool batch.
120    #[must_use]
121    pub fn with_max_parallel_tool_calls(mut self, limit: NonZeroUsize) -> Self {
122        self.max_parallel_tool_calls = Some(limit);
123        self
124    }
125
126    /// Replaces the live permission review mode for subsequent tool executions.
127    #[must_use]
128    pub fn with_permission_review_mode(mut self, mode: PermissionReviewMode) -> Self {
129        self.permission_review_mode = Some(mode);
130        self
131    }
132
133    /// Supplies or replaces the host permission admission source used by host-reviewed modes.
134    #[must_use]
135    pub fn with_permission_admission_source(
136        mut self,
137        source: Arc<dyn PermissionAdmissionSource>,
138    ) -> Self {
139        self.permission_admission_source = Some(source);
140        self
141    }
142}