zeph-commands 0.19.0

Slash command registry, handler trait, and channel sink abstraction for Zeph
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Slash command registry, handler trait, and channel sink abstraction for Zeph.
//!
//! This crate provides the non-generic infrastructure for slash command dispatch:
//! - [`ChannelSink`] — minimal async I/O trait replacing the `C: Channel` generic in handlers
//! - [`CommandOutput`] — exhaustive result type for command execution
//! - [`SlashCategory`] — grouping enum for `/help` output
//! - [`CommandInfo`] — static metadata for a registered command
//! - [`CommandHandler`] — object-safe handler trait (no `C` generic)
//! - [`CommandRegistry`] — registry with longest-word-boundary dispatch
//! - [`CommandContext`] — non-generic dispatch context with trait-object fields
//! - [`traits`] — sub-trait definitions for subsystem access
//! - [`handlers`] — concrete handler implementations (session, debug)
//!
//! # Design
//!
//! `CommandRegistry` and `CommandHandler` are non-generic: they operate on [`CommandContext`],
//! a concrete struct whose fields are trait objects (`&mut dyn DebugAccess`, etc.). `zeph-core`
//! implements these traits on its internal state types and constructs `CommandContext` at dispatch
//! time from `Agent<C>` fields.
//!
//! This crate does NOT depend on `zeph-core`. A change in `zeph-core`'s agent loop does
//! not recompile `zeph-commands`.

pub mod commands;
pub mod context;
pub mod handlers;
pub mod sink;
pub mod traits;

pub use commands::COMMANDS;

pub use context::CommandContext;
pub use sink::{ChannelSink, NullSink};
pub use traits::agent::{AgentAccess, NullAgent};

use std::future::Future;
use std::pin::Pin;

/// Result of executing a slash command.
///
/// Replaces the heterogeneous return types of earlier command dispatch with a unified,
/// exhaustive enum.
#[derive(Debug)]
pub enum CommandOutput {
    /// Send a message to the user via the channel.
    Message(String),
    /// Command handled silently; no output (e.g., `/clear`).
    Silent,
    /// Exit the agent loop immediately.
    Exit,
    /// Continue to the next loop iteration.
    Continue,
}

/// Category for grouping commands in `/help` output.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SlashCategory {
    /// Session management: `/clear`, `/reset`, `/exit`, etc.
    Session,
    /// Model and provider configuration: `/model`, `/provider`, `/guardrail`, etc.
    Configuration,
    /// Memory and knowledge: `/memory`, `/graph`, `/compact`, etc.
    Memory,
    /// Skill management: `/skill`, `/skills`, etc.
    Skills,
    /// Planning and focus: `/plan`, `/focus`, `/sidequest`, etc.
    Planning,
    /// Debugging and diagnostics: `/debug-dump`, `/log`, `/lsp`, etc.
    Debugging,
    /// External integrations: `/mcp`, `/image`, `/agent`, etc.
    Integration,
    /// Advanced and experimental: `/experiment`, `/policy`, `/scheduler`, etc.
    Advanced,
}

impl SlashCategory {
    /// Return the display label for this category in `/help` output.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Session => "Session",
            Self::Configuration => "Configuration",
            Self::Memory => "Memory",
            Self::Skills => "Skills",
            Self::Planning => "Planning",
            Self::Debugging => "Debugging",
            Self::Integration => "Integration",
            Self::Advanced => "Advanced",
        }
    }
}

/// Static metadata about a registered command, used for `/help` output generation.
pub struct CommandInfo {
    /// Command name including the leading slash, e.g. `"/help"`.
    pub name: &'static str,
    /// Argument hint shown after the command name in help, e.g. `"[path]"`.
    pub args: &'static str,
    /// One-line description shown in `/help` output.
    pub description: &'static str,
    /// Category for grouping in `/help`.
    pub category: SlashCategory,
    /// Feature gate label, if this command is conditionally compiled.
    pub feature_gate: Option<&'static str>,
}

/// Error type returned by command handlers.
///
/// Wraps agent-level errors as a string to avoid depending on `zeph-core`'s `AgentError`.
/// `zeph-core` converts between `AgentError` and `CommandError` at the dispatch boundary.
#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub struct CommandError(pub String);

impl CommandError {
    /// Create a `CommandError` from any displayable value.
    pub fn new(msg: impl std::fmt::Display) -> Self {
        Self(msg.to_string())
    }
}

/// A slash command handler that can be registered with [`CommandRegistry`].
///
/// Implementors must be `Send + Sync` because the registry is constructed at agent
/// initialization time and handlers may be invoked from async contexts.
///
/// # Object safety
///
/// The `handle` method uses `Pin<Box<dyn Future>>` instead of `async fn` to remain
/// object-safe, enabling the registry to store `Box<dyn CommandHandler<Ctx>>`. Slash
/// commands are user-initiated so the box allocation is negligible.
pub trait CommandHandler<Ctx: ?Sized>: Send + Sync {
    /// Command name including the leading slash, e.g. `"/help"`.
    ///
    /// Must be unique per registry. Used as the dispatch key.
    fn name(&self) -> &'static str;

    /// One-line description shown in `/help` output.
    fn description(&self) -> &'static str;

    /// Argument hint shown after the command name in help, e.g. `"[path]"`.
    ///
    /// Return an empty string if the command takes no arguments.
    fn args_hint(&self) -> &'static str {
        ""
    }

    /// Category for grouping in `/help`.
    fn category(&self) -> SlashCategory;

    /// Feature gate label, if this command is conditionally compiled.
    fn feature_gate(&self) -> Option<&'static str> {
        None
    }

    /// Execute the command.
    ///
    /// # Arguments
    ///
    /// - `ctx`: Typed access to agent subsystems.
    /// - `args`: Trimmed text after the command name. Empty string when no args given.
    ///
    /// # Errors
    ///
    /// Returns `Err(CommandError)` when the command fails. The dispatch site logs and
    /// reports the error to the user.
    fn handle<'a>(
        &'a self,
        ctx: &'a mut Ctx,
        args: &'a str,
    ) -> Pin<Box<dyn Future<Output = Result<CommandOutput, CommandError>> + Send + 'a>>;
}

/// Registry of slash command handlers.
///
/// Handlers are stored in a `Vec`, not a `HashMap`, because command count is small (< 40)
/// and registration happens once at agent initialization. Dispatch performs a linear scan
/// with longest-word-boundary match to support subcommands.
///
/// # Dispatch
///
/// See [`CommandRegistry::dispatch`] for the full dispatch algorithm.
///
/// # Borrow splitting
///
/// When stored as an `Agent<C>` field, the dispatch call site uses `std::mem::take` to
/// temporarily move the registry out of the agent, construct a context, dispatch, and
/// restore the registry. This avoids borrow-checker conflicts.
pub struct CommandRegistry<Ctx: ?Sized> {
    handlers: Vec<Box<dyn CommandHandler<Ctx>>>,
}

impl<Ctx: ?Sized> CommandRegistry<Ctx> {
    /// Create an empty registry.
    #[must_use]
    pub fn new() -> Self {
        Self {
            handlers: Vec::new(),
        }
    }

    /// Register a command handler.
    ///
    /// # Panics
    ///
    /// Panics if a handler with the same name is already registered.
    pub fn register(&mut self, handler: impl CommandHandler<Ctx> + 'static) {
        let name = handler.name();
        assert!(
            !self.handlers.iter().any(|h| h.name() == name),
            "duplicate command name: {name}"
        );
        self.handlers.push(Box::new(handler));
    }

    /// Dispatch a command string to the matching handler.
    ///
    /// Returns `None` if the input does not start with `/` or no handler matches.
    ///
    /// # Algorithm
    ///
    /// 1. Return `None` if `input` does not start with `/`.
    /// 2. Find all handlers where `input == name` or `input.starts_with(name + " ")`.
    /// 3. Pick the handler with the longest matching name (subcommand resolution).
    /// 4. Extract `args = input[name.len()..].trim()`.
    /// 5. Call `handler.handle(ctx, args)` and return the result.
    ///
    /// # Errors
    ///
    /// Returns `Some(Err(_))` when the matched handler returns an error.
    pub async fn dispatch(
        &self,
        ctx: &mut Ctx,
        input: &str,
    ) -> Option<Result<CommandOutput, CommandError>> {
        let trimmed = input.trim();
        if !trimmed.starts_with('/') {
            return None;
        }

        let mut best_len: usize = 0;
        let mut best_idx: Option<usize> = None;
        for (idx, handler) in self.handlers.iter().enumerate() {
            let name = handler.name();
            let matched = trimmed == name
                || trimmed
                    .strip_prefix(name)
                    .is_some_and(|rest| rest.starts_with(' '));
            if matched && name.len() >= best_len {
                best_len = name.len();
                best_idx = Some(idx);
            }
        }

        let handler = &self.handlers[best_idx?];
        let name = handler.name();
        let args = trimmed[name.len()..].trim();
        Some(handler.handle(ctx, args).await)
    }

    /// Find the handler that would be selected for the given input, without dispatching.
    ///
    /// Returns `Some((idx, name))` or `None` if no handler matches.
    /// Primarily used in tests to verify routing.
    #[must_use]
    pub fn find_handler(&self, input: &str) -> Option<(usize, &'static str)> {
        let trimmed = input.trim();
        if !trimmed.starts_with('/') {
            return None;
        }
        let mut best_len: usize = 0;
        let mut best: Option<(usize, &'static str)> = None;
        for (idx, handler) in self.handlers.iter().enumerate() {
            let name = handler.name();
            let matched = trimmed == name
                || trimmed
                    .strip_prefix(name)
                    .is_some_and(|rest| rest.starts_with(' '));
            if matched && name.len() >= best_len {
                best_len = name.len();
                best = Some((idx, name));
            }
        }
        best
    }

    /// List all registered commands for `/help` generation.
    ///
    /// Returns metadata in registration order.
    #[must_use]
    pub fn list(&self) -> Vec<CommandInfo> {
        self.handlers
            .iter()
            .map(|h| CommandInfo {
                name: h.name(),
                args: h.args_hint(),
                description: h.description(),
                category: h.category(),
                feature_gate: h.feature_gate(),
            })
            .collect()
    }
}

impl<Ctx: ?Sized> Default for CommandRegistry<Ctx> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::future::Future;
    use std::pin::Pin;

    struct MockCtx;

    struct FixedHandler {
        name: &'static str,
        category: SlashCategory,
    }

    impl CommandHandler<MockCtx> for FixedHandler {
        fn name(&self) -> &'static str {
            self.name
        }

        fn description(&self) -> &'static str {
            "test handler"
        }

        fn category(&self) -> SlashCategory {
            self.category
        }

        fn handle<'a>(
            &'a self,
            _ctx: &'a mut MockCtx,
            args: &'a str,
        ) -> Pin<Box<dyn Future<Output = Result<CommandOutput, CommandError>> + Send + 'a>>
        {
            let name = self.name;
            Box::pin(async move { Ok(CommandOutput::Message(format!("{name}:{args}"))) })
        }
    }

    fn make_handler(name: &'static str) -> FixedHandler {
        FixedHandler {
            name,
            category: SlashCategory::Session,
        }
    }

    #[tokio::test]
    async fn dispatch_routes_longest_match() {
        let mut reg: CommandRegistry<MockCtx> = CommandRegistry::new();
        reg.register(make_handler("/plan"));
        reg.register(make_handler("/plan confirm"));

        let mut ctx = MockCtx;
        let out = reg
            .dispatch(&mut ctx, "/plan confirm foo")
            .await
            .unwrap()
            .unwrap();
        let CommandOutput::Message(msg) = out else {
            panic!("expected Message");
        };
        assert_eq!(msg, "/plan confirm:foo");
    }

    #[tokio::test]
    async fn dispatch_returns_none_for_non_slash() {
        let mut reg: CommandRegistry<MockCtx> = CommandRegistry::new();
        reg.register(make_handler("/help"));
        let mut ctx = MockCtx;
        assert!(reg.dispatch(&mut ctx, "hello").await.is_none());
    }

    #[tokio::test]
    async fn dispatch_returns_none_for_unregistered() {
        let mut reg: CommandRegistry<MockCtx> = CommandRegistry::new();
        reg.register(make_handler("/help"));
        let mut ctx = MockCtx;
        assert!(reg.dispatch(&mut ctx, "/unknown").await.is_none());
    }

    #[test]
    #[should_panic(expected = "duplicate command name")]
    fn register_panics_on_duplicate() {
        let mut reg: CommandRegistry<MockCtx> = CommandRegistry::new();
        reg.register(make_handler("/plan"));
        reg.register(make_handler("/plan"));
    }

    #[test]
    fn list_returns_metadata_in_order() {
        let mut reg: CommandRegistry<MockCtx> = CommandRegistry::new();
        reg.register(make_handler("/alpha"));
        reg.register(make_handler("/beta"));
        let list = reg.list();
        assert_eq!(list.len(), 2);
        assert_eq!(list[0].name, "/alpha");
        assert_eq!(list[1].name, "/beta");
    }

    #[test]
    fn slash_category_as_str_all_variants() {
        let variants = [
            (SlashCategory::Session, "Session"),
            (SlashCategory::Configuration, "Configuration"),
            (SlashCategory::Memory, "Memory"),
            (SlashCategory::Skills, "Skills"),
            (SlashCategory::Planning, "Planning"),
            (SlashCategory::Debugging, "Debugging"),
            (SlashCategory::Integration, "Integration"),
            (SlashCategory::Advanced, "Advanced"),
        ];
        for (variant, expected) in variants {
            assert_eq!(variant.as_str(), expected);
        }
    }
}