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