Skip to main content

merry_runtime/interactive/
settings.rs

1use crate::{
2    AcceptedLocalWorkspaceProcessAdmission, CompactionConfig, PermissionAdmissionSource,
3    PermissionReviewMode, PermissionedProcessRunnerFactory, ProcessRunner, RuntimeModelRole,
4    SubagentConfig,
5};
6use merry_llm::{GenerationConfig, ModelName, ModelProvider, ModelRetryPolicy, ReasoningEffort};
7use std::{
8    num::{NonZeroU64, NonZeroUsize},
9    sync::Arc,
10};
11
12/// Provider-neutral model selection for subsequent interactive requests.
13#[derive(Clone)]
14pub struct InteractivePrimaryModel {
15    pub(crate) provider: Arc<dyn ModelProvider>,
16    pub(crate) model: ModelName,
17    pub(crate) retry_policy: ModelRetryPolicy,
18    pub(crate) reasoning_effort: Option<ReasoningEffort>,
19}
20
21/// Subagent admission and concurrency settings for subsequent delegated work.
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub struct InteractiveSubagentSettings {
24    pub(super) enabled: bool,
25    pub(super) config: SubagentConfig,
26}
27
28impl InteractiveSubagentSettings {
29    #[must_use]
30    pub fn new(enabled: bool, config: SubagentConfig) -> Self {
31        Self { enabled, config }
32    }
33}
34
35impl InteractivePrimaryModel {
36    #[must_use]
37    pub fn new(
38        provider: Arc<dyn ModelProvider>,
39        model: ModelName,
40        retry_policy: ModelRetryPolicy,
41    ) -> Self {
42        Self {
43            provider,
44            model,
45            retry_policy,
46            reasoning_effort: None,
47        }
48    }
49
50    /// Attaches an optional role-specific reasoning effort override.
51    #[must_use]
52    pub fn with_reasoning_effort(mut self, reasoning_effort: Option<ReasoningEffort>) -> Self {
53        self.reasoning_effort = reasoning_effort;
54        self
55    }
56}
57
58/// Replacement local-workspace process execution lane for subsequent tool
59/// actions in an active interactive run.
60///
61/// Swapping the process runner, admission, and permissioned factory lets the
62/// host toggle sandbox mode or host integrations without restarting the session
63/// or tearing down retained background processes.
64#[derive(Clone)]
65pub struct InteractiveProcessExecutionUpdate {
66    pub(crate) admission: AcceptedLocalWorkspaceProcessAdmission,
67    pub(crate) runner: Arc<dyn ProcessRunner>,
68    pub(crate) permissioned_factory: Arc<dyn PermissionedProcessRunnerFactory>,
69}
70
71impl InteractiveProcessExecutionUpdate {
72    /// Creates a replacement process execution lane update.
73    #[must_use]
74    pub fn new(
75        admission: AcceptedLocalWorkspaceProcessAdmission,
76        runner: Arc<dyn ProcessRunner>,
77        permissioned_factory: Arc<dyn PermissionedProcessRunnerFactory>,
78    ) -> Self {
79        Self {
80            admission,
81            runner,
82            permissioned_factory,
83        }
84    }
85}
86
87/// Runtime settings applied by an interactive run at the next model-request boundary.
88#[derive(Clone, Default)]
89pub struct InteractiveSettingsUpdate {
90    pub(super) generation_config: Option<GenerationConfig>,
91    pub(super) primary_model: Option<InteractivePrimaryModel>,
92    pub(super) role_models: Vec<(RuntimeModelRole, Option<InteractivePrimaryModel>)>,
93    pub(super) subagents: Option<InteractiveSubagentSettings>,
94    pub(super) automatic_compaction: Option<CompactionConfig>,
95    pub(super) context_window_tokens: Option<Option<NonZeroU64>>,
96    pub(super) max_parallel_tool_calls: Option<NonZeroUsize>,
97    pub(super) permission_review_mode: Option<PermissionReviewMode>,
98    pub(super) permission_admission_source: Option<Arc<dyn PermissionAdmissionSource>>,
99    pub(super) process_execution: Option<InteractiveProcessExecutionUpdate>,
100}
101
102impl InteractiveSettingsUpdate {
103    /// Replaces the generation configuration used by subsequent model requests.
104    #[must_use]
105    pub fn with_generation_config(mut self, generation_config: GenerationConfig) -> Self {
106        self.generation_config = Some(generation_config);
107        self
108    }
109
110    /// Replaces the primary provider and model used by subsequent model requests.
111    #[must_use]
112    pub fn with_primary_model(mut self, primary_model: InteractivePrimaryModel) -> Self {
113        self.primary_model = Some(primary_model);
114        self
115    }
116
117    /// Replaces or clears a role-specific provider/model/effort override (such as
118    /// `ContextCompaction` or `ApprovalReview`). Passing `None` falls back to primary.
119    #[must_use]
120    pub fn with_role_model(
121        mut self,
122        role: RuntimeModelRole,
123        model: Option<InteractivePrimaryModel>,
124    ) -> Self {
125        self.role_models.push((role, model));
126        self
127    }
128
129    /// Replaces subagent admission and concurrency settings.
130    #[must_use]
131    pub fn with_subagents(mut self, subagents: InteractiveSubagentSettings) -> Self {
132        self.subagents = Some(subagents);
133        self
134    }
135
136    /// Replaces automatic compaction policy for subsequent model requests.
137    #[must_use]
138    pub fn with_automatic_compaction(mut self, automatic_compaction: CompactionConfig) -> Self {
139        self.automatic_compaction = Some(automatic_compaction);
140        self
141    }
142
143    /// Replaces or clears the explicit context-window override for subsequent requests.
144    #[must_use]
145    pub fn with_context_window_tokens(mut self, context_window_tokens: Option<NonZeroU64>) -> Self {
146        self.context_window_tokens = Some(context_window_tokens);
147        self
148    }
149
150    /// Replaces the maximum concurrent parallel tool calls per tool batch.
151    #[must_use]
152    pub fn with_max_parallel_tool_calls(mut self, limit: NonZeroUsize) -> Self {
153        self.max_parallel_tool_calls = Some(limit);
154        self
155    }
156
157    /// Replaces the live permission review mode for subsequent tool executions.
158    #[must_use]
159    pub fn with_permission_review_mode(mut self, mode: PermissionReviewMode) -> Self {
160        self.permission_review_mode = Some(mode);
161        self
162    }
163
164    /// Supplies or replaces the host permission admission source used by host-reviewed modes.
165    #[must_use]
166    pub fn with_permission_admission_source(
167        mut self,
168        source: Arc<dyn PermissionAdmissionSource>,
169    ) -> Self {
170        self.permission_admission_source = Some(source);
171        self
172    }
173
174    /// Replaces the local-workspace process runner, admission, and permissioned
175    /// factory used by subsequent process tool executions.
176    #[must_use]
177    pub fn with_process_execution(
178        mut self,
179        process_execution: InteractiveProcessExecutionUpdate,
180    ) -> Self {
181        self.process_execution = Some(process_execution);
182        self
183    }
184}