Skip to main content

codewhale_command_contract/
handler.rs

1//! Generic handler transport for staged command migration.
2//!
3//! The output type is generic so FEAT-014 does not move or duplicate the
4//! TUI-owned `CommandResult`. During in-place adoption, the TUI instantiates
5//! `CommandHandler<crate::commands::CommandResult>`.
6
7use crate::facets::{
8    CommandCostContext, CommandMediaContext, CommandMemoryContext, CommandModePolicyContext,
9    CommandModelContext, CommandPluginContext, CommandPresentationContext, CommandProjectContext,
10    CommandSessionContext, CommandSessionControlContext, CommandSessionLifecycleContext,
11    CommandSkillGroupContext, CommandSkillsContext, CommandSystemPromptContext,
12    CommandWorkspaceContext,
13};
14
15/// Exact host capabilities exposed to one contextual command handler.
16///
17/// The set lives in the external contract crate so command registrations can
18/// declare least authority without naming the TUI host. The dispatcher uses
19/// the declaration to populate only those slots in [`CommandContexts`].
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
21pub struct CommandCapabilities(u16);
22
23impl CommandCapabilities {
24    pub const NONE: Self = Self(0);
25    pub const SESSION: Self = Self(1 << 0);
26    pub const MODEL: Self = Self(1 << 1);
27    pub const COST: Self = Self(1 << 2);
28    pub const MODE_POLICY: Self = Self(1 << 3);
29    pub const SYSTEM_PROMPT: Self = Self(1 << 4);
30    pub const SKILLS: Self = Self(1 << 5);
31    pub const WORKSPACE: Self = Self(1 << 6);
32    pub const PRESENTATION: Self = Self(1 << 7);
33    pub const MEDIA: Self = Self(1 << 8);
34    /// Memory-group host data (FEAT-019 D1).
35    pub const MEMORY: Self = Self(1 << 9);
36    /// Project-group host data (FEAT-021 D1).
37    pub const PROJECT: Self = Self(1 << 10);
38    /// Skills-group host data (FEAT-022 D1).
39    pub const SKILL_GROUP: Self = Self(1 << 11);
40    /// Plugin-group host data (FEAT-020 D1), appended after current main capabilities.
41    pub const PLUGIN: Self = Self(1 << 12);
42    /// Session-lifecycle host data (FEAT-023 D3), the next non-conflicting bit
43    /// after `PLUGIN`. Required only by the seven host-dependent lifecycle
44    /// commands; `/compact` and `/purge` remain pure. Never widened by the
45    /// basic session capability.
46    pub const SESSION_LIFECYCLE: Self = Self(1 << 13);
47    /// Session-control host data (FEAT-024 D3), the next non-conflicting bit
48    /// after `SESSION_LIFECYCLE`. Required only by the six host-dependent
49    /// control commands (`/relay`, `/rename`, `/resume`, `/rc`, `/remote-env`,
50    /// `/title`); `/remote-env` also declares `PRESENTATION`. The backing
51    /// storage remains `u16` per the resolved maintainer review on FEAT-023 PR
52    /// #5902 — bit 14 is available, so no speculative widening is performed.
53    pub const SESSION_CONTROL: Self = Self(1 << 14);
54
55    pub const fn union(self, other: Self) -> Self {
56        Self(self.0 | other.0)
57    }
58
59    pub const fn contains(self, capability: Self) -> bool {
60        !capability.is_empty() && self.0 & capability.0 == capability.0
61    }
62
63    pub const fn is_empty(self) -> bool {
64        self.0 == 0
65    }
66}
67
68impl std::ops::BitOr for CommandCapabilities {
69    type Output = Self;
70
71    fn bitor(self, rhs: Self) -> Self::Output {
72        self.union(rhs)
73    }
74}
75
76/// A command handler that is either argument-only or capability-scoped.
77#[derive(Clone, Copy)]
78pub enum CommandHandler<R> {
79    Pure(fn(Option<&str>) -> R),
80    Contextual {
81        capabilities: CommandCapabilities,
82        handler: fn(CommandContexts<'_>, Option<&str>) -> R,
83    },
84}
85
86/// Transport envelope with one independently optional facet slot.
87pub struct CommandContexts<'a> {
88    session: Option<&'a mut dyn CommandSessionContext>,
89    model: Option<&'a mut dyn CommandModelContext>,
90    cost: Option<&'a mut dyn CommandCostContext>,
91    mode_policy: Option<&'a mut dyn CommandModePolicyContext>,
92    system_prompt: Option<&'a mut dyn CommandSystemPromptContext>,
93    skills: Option<&'a mut dyn CommandSkillsContext>,
94    workspace: Option<&'a mut dyn CommandWorkspaceContext>,
95    presentation: Option<&'a mut dyn CommandPresentationContext>,
96    media: Option<&'a mut dyn CommandMediaContext>,
97    memory: Option<&'a mut dyn CommandMemoryContext>,
98    project: Option<&'a mut dyn CommandProjectContext>,
99    skill_group: Option<&'a mut dyn CommandSkillGroupContext>,
100    plugin: Option<&'a mut dyn CommandPluginContext>,
101    lifecycle: Option<&'a mut dyn CommandSessionLifecycleContext>,
102    control: Option<&'a mut dyn CommandSessionControlContext>,
103}
104
105/// Consumed envelope used when one handler needs several independent facets.
106pub struct ContextParts<'a> {
107    pub session: Option<&'a mut dyn CommandSessionContext>,
108    pub model: Option<&'a mut dyn CommandModelContext>,
109    pub cost: Option<&'a mut dyn CommandCostContext>,
110    pub mode_policy: Option<&'a mut dyn CommandModePolicyContext>,
111    pub system_prompt: Option<&'a mut dyn CommandSystemPromptContext>,
112    pub skills: Option<&'a mut dyn CommandSkillsContext>,
113    pub workspace: Option<&'a mut dyn CommandWorkspaceContext>,
114    pub presentation: Option<&'a mut dyn CommandPresentationContext>,
115    pub media: Option<&'a mut dyn CommandMediaContext>,
116    pub memory: Option<&'a mut dyn CommandMemoryContext>,
117    pub project: Option<&'a mut dyn CommandProjectContext>,
118    pub skill_group: Option<&'a mut dyn CommandSkillGroupContext>,
119    pub plugin: Option<&'a mut dyn CommandPluginContext>,
120    pub lifecycle: Option<&'a mut dyn CommandSessionLifecycleContext>,
121    pub control: Option<&'a mut dyn CommandSessionControlContext>,
122}
123
124impl<'a> CommandContexts<'a> {
125    pub fn empty() -> Self {
126        Self {
127            session: None,
128            model: None,
129            cost: None,
130            mode_policy: None,
131            system_prompt: None,
132            skills: None,
133            workspace: None,
134            presentation: None,
135            media: None,
136            memory: None,
137            project: None,
138            skill_group: None,
139            plugin: None,
140            lifecycle: None,
141            control: None,
142        }
143    }
144
145    pub fn into_parts(self) -> ContextParts<'a> {
146        ContextParts {
147            session: self.session,
148            model: self.model,
149            cost: self.cost,
150            mode_policy: self.mode_policy,
151            system_prompt: self.system_prompt,
152            skills: self.skills,
153            workspace: self.workspace,
154            presentation: self.presentation,
155            media: self.media,
156            memory: self.memory,
157            project: self.project,
158            skill_group: self.skill_group,
159            plugin: self.plugin,
160            lifecycle: self.lifecycle,
161            control: self.control,
162        }
163    }
164
165    pub fn with_session(mut self, value: &'a mut dyn CommandSessionContext) -> Self {
166        assert!(
167            self.session.replace(value).is_none(),
168            "session facet already set"
169        );
170        self
171    }
172
173    pub fn with_model(mut self, value: &'a mut dyn CommandModelContext) -> Self {
174        assert!(
175            self.model.replace(value).is_none(),
176            "model facet already set"
177        );
178        self
179    }
180
181    pub fn with_cost(mut self, value: &'a mut dyn CommandCostContext) -> Self {
182        assert!(self.cost.replace(value).is_none(), "cost facet already set");
183        self
184    }
185
186    pub fn with_mode_policy(mut self, value: &'a mut dyn CommandModePolicyContext) -> Self {
187        assert!(
188            self.mode_policy.replace(value).is_none(),
189            "mode-policy facet already set"
190        );
191        self
192    }
193
194    pub fn with_system_prompt(mut self, value: &'a mut dyn CommandSystemPromptContext) -> Self {
195        assert!(
196            self.system_prompt.replace(value).is_none(),
197            "system-prompt facet already set"
198        );
199        self
200    }
201
202    pub fn with_skills(mut self, value: &'a mut dyn CommandSkillsContext) -> Self {
203        assert!(
204            self.skills.replace(value).is_none(),
205            "skills facet already set"
206        );
207        self
208    }
209
210    pub fn with_workspace(mut self, value: &'a mut dyn CommandWorkspaceContext) -> Self {
211        assert!(
212            self.workspace.replace(value).is_none(),
213            "workspace facet already set"
214        );
215        self
216    }
217
218    pub fn with_presentation(mut self, value: &'a mut dyn CommandPresentationContext) -> Self {
219        assert!(
220            self.presentation.replace(value).is_none(),
221            "presentation facet already set"
222        );
223        self
224    }
225
226    pub fn with_media(mut self, value: &'a mut dyn CommandMediaContext) -> Self {
227        assert!(
228            self.media.replace(value).is_none(),
229            "media facet already set"
230        );
231        self
232    }
233
234    pub fn with_memory(mut self, value: &'a mut dyn CommandMemoryContext) -> Self {
235        assert!(
236            self.memory.replace(value).is_none(),
237            "memory facet already set"
238        );
239        self
240    }
241
242    pub fn with_project(mut self, value: &'a mut dyn CommandProjectContext) -> Self {
243        assert!(
244            self.project.replace(value).is_none(),
245            "project facet already set"
246        );
247        self
248    }
249
250    pub fn with_skill_group(mut self, value: &'a mut dyn CommandSkillGroupContext) -> Self {
251        assert!(
252            self.skill_group.replace(value).is_none(),
253            "skill-group facet already set"
254        );
255        self
256    }
257
258    pub fn with_plugin(mut self, value: &'a mut dyn CommandPluginContext) -> Self {
259        assert!(
260            self.plugin.replace(value).is_none(),
261            "plugin facet already set"
262        );
263        self
264    }
265
266    pub fn with_lifecycle(mut self, value: &'a mut dyn CommandSessionLifecycleContext) -> Self {
267        assert!(
268            self.lifecycle.replace(value).is_none(),
269            "lifecycle facet already set"
270        );
271        self
272    }
273
274    pub fn with_control(mut self, value: &'a mut dyn CommandSessionControlContext) -> Self {
275        assert!(
276            self.control.replace(value).is_none(),
277            "control facet already set"
278        );
279        self
280    }
281}
282
283impl Default for CommandContexts<'_> {
284    fn default() -> Self {
285        Self::empty()
286    }
287}