ricecoder 0.1.72

Terminal-first, spec-driven coding assistant that understands your project before generating code
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// Command routing and dispatch
// Adapted from automation/src/cli/router.rs

use crate::commands::*;
use crate::error::{CliError, CliResult};
use clap::{Parser, Subcommand};

/// RiceCoder - Terminal-first, spec-driven coding assistant
#[derive(Parser, Debug)]
#[command(name = "rice")]
#[command(bin_name = "rice")]
#[command(about = "Terminal-first, spec-driven coding assistant")]
#[command(
    long_about = "RiceCoder: A terminal-first, spec-driven coding assistant.\n\nGenerate code from specifications, refactor existing code, and get AI-powered code reviews.\n\nFor more information, visit: https://ricecoder.dev"
)]
#[command(version)]
#[command(author = "RiceCoder Contributors")]
#[command(arg_required_else_help = true)]
#[command(disable_help_subcommand = true)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,

    /// Enable verbose output
    #[arg(short, long, global = true)]
    pub verbose: bool,

    /// Minimize output
    #[arg(short, long, global = true)]
    pub quiet: bool,

    /// Preview changes without applying them
    #[arg(long, global = true)]
    pub dry_run: bool,
}

#[derive(Subcommand, Debug)]
pub enum Commands {
    /// Initialize a new ricecoder project
    #[command(about = "Initialize a new ricecoder project with default configuration")]
    Init {
        /// Project path (default: current directory)
        #[arg(value_name = "PATH")]
        path: Option<String>,
    },

    /// Generate code from a specification
    #[command(about = "Generate code from a specification file")]
    Gen {
        /// Path to specification file
        #[arg(value_name = "SPEC")]
        spec: String,
    },

    /// Interactive chat mode with spec awareness
    #[command(about = "Enter interactive chat mode for free-form coding assistance")]
    Chat {
        /// Initial message to send
        #[arg(value_name = "MESSAGE")]
        message: Option<String>,

        /// AI provider to use (openai, anthropic, local)
        #[arg(short, long)]
        provider: Option<String>,

        /// Model to use
        #[arg(short, long)]
        model: Option<String>,
    },

    /// Refactor existing code
    #[command(about = "Refactor existing code using AI assistance")]
    Refactor {
        /// File to refactor
        #[arg(value_name = "FILE")]
        file: String,
    },

    /// Review code for improvements
    #[command(about = "Review code for improvements and best practices")]
    Review {
        /// File to review
        #[arg(value_name = "FILE")]
        file: String,
    },

    /// Manage configuration settings
    #[command(about = "View and manage ricecoder configuration")]
    Config {
        #[command(subcommand)]
        action: Option<ConfigSubcommand>,
    },

    /// Generate shell completions
    #[command(about = "Generate shell completion scripts")]
    Completions {
        /// Shell to generate completions for (bash, zsh, fish, powershell)
        #[arg(value_name = "SHELL")]
        shell: String,
    },

    /// Manage and execute custom commands
    #[command(about = "Manage and execute custom commands")]
    Custom {
        #[command(subcommand)]
        action: Option<CustomSubcommand>,
    },

    /// Launch the terminal user interface
    #[command(about = "Launch the beautiful terminal user interface")]
    Tui {
        /// Theme to use (dark, light, monokai, dracula, nord)
        #[arg(short, long)]
        theme: Option<String>,

        /// Enable vim keybindings
        #[arg(long)]
        vim_mode: bool,

        /// Custom config file path
        #[arg(short, long)]
        config: Option<String>,

        /// AI provider to use (openai, anthropic, local)
        #[arg(short, long)]
        provider: Option<String>,

        /// Model to use
        #[arg(short, long)]
        model: Option<String>,
    },

    /// Manage sessions
    #[command(about = "Manage ricecoder sessions")]
    Sessions {
        #[command(subcommand)]
        action: Option<SessionsSubcommand>,
    },

    /// Start the Language Server Protocol server
    #[command(about = "Start the Language Server Protocol server for IDE integration")]
    Lsp {
        /// Log level (trace, debug, info, warn, error)
        #[arg(short, long, default_value = "info")]
        log_level: Option<String>,

        /// Port for TCP transport (future support)
        #[arg(short, long)]
        port: Option<u16>,

        /// Enable debug mode for verbose logging
        #[arg(long)]
        debug: bool,
    },

    /// Manage hooks for event-driven automation
    #[command(about = "Manage hooks for event-driven automation")]
    Hooks {
        #[command(subcommand)]
        action: Option<HooksSubcommand>,
    },

    /// Show help and tutorials
    #[command(about = "Show help, tutorials, and troubleshooting guides")]
    Help {
        /// Topic to get help on (command name, 'tutorial', 'troubleshooting')
        #[arg(value_name = "TOPIC")]
        topic: Option<String>,
    },
}

#[derive(Subcommand, Debug)]
pub enum HooksSubcommand {
    /// List all hooks
    #[command(about = "List all registered hooks")]
    List {
        /// Output format (table or json)
        #[arg(short, long)]
        format: Option<String>,
    },

    /// Inspect a specific hook
    #[command(about = "Inspect a specific hook")]
    Inspect {
        /// Hook ID
        #[arg(value_name = "ID")]
        id: String,

        /// Output format (table or json)
        #[arg(short, long)]
        format: Option<String>,
    },

    /// Enable a hook
    #[command(about = "Enable a hook")]
    Enable {
        /// Hook ID
        #[arg(value_name = "ID")]
        id: String,
    },

    /// Disable a hook
    #[command(about = "Disable a hook")]
    Disable {
        /// Hook ID
        #[arg(value_name = "ID")]
        id: String,
    },

    /// Delete a hook
    #[command(about = "Delete a hook")]
    Delete {
        /// Hook ID
        #[arg(value_name = "ID")]
        id: String,
    },
}

#[derive(Subcommand, Debug)]
pub enum SessionsSubcommand {
    /// List all sessions
    #[command(about = "List all sessions")]
    List,

    /// Create a new session
    #[command(about = "Create a new session")]
    Create {
        /// Session name
        #[arg(value_name = "NAME")]
        name: String,
    },

    /// Delete a session
    #[command(about = "Delete a session")]
    Delete {
        /// Session ID
        #[arg(value_name = "ID")]
        id: String,
    },

    /// Rename a session
    #[command(about = "Rename a session")]
    Rename {
        /// Session ID
        #[arg(value_name = "ID")]
        id: String,

        /// New session name
        #[arg(value_name = "NAME")]
        name: String,
    },

    /// Switch to a session
    #[command(about = "Switch to a session")]
    Switch {
        /// Session ID
        #[arg(value_name = "ID")]
        id: String,
    },

    /// Show session info
    #[command(about = "Show session information")]
    Info {
        /// Session ID
        #[arg(value_name = "ID")]
        id: String,
    },

    /// Share a session with a shareable link
    #[command(about = "Generate a shareable link for the current session")]
    Share {
        /// Expiration time in seconds (optional)
        #[arg(long)]
        expires_in: Option<u64>,

        /// Exclude conversation history from share
        #[arg(long)]
        no_history: bool,

        /// Exclude project context from share
        #[arg(long)]
        no_context: bool,
    },

    /// List all active shares
    #[command(about = "List all active shares for the current user")]
    ShareList,

    /// Revoke a share
    #[command(about = "Revoke a share by ID")]
    ShareRevoke {
        /// Share ID to revoke
        #[arg(value_name = "SHARE_ID")]
        share_id: String,
    },

    /// Show share information
    #[command(about = "Show detailed information about a share")]
    ShareInfo {
        /// Share ID
        #[arg(value_name = "SHARE_ID")]
        share_id: String,
    },

    /// View a shared session
    #[command(about = "View a shared session by share ID")]
    ShareView {
        /// Share ID to view
        #[arg(value_name = "SHARE_ID")]
        share_id: String,
    },
}

#[derive(Subcommand, Debug)]
pub enum CustomSubcommand {
    /// List all available custom commands
    #[command(about = "Display all available custom commands")]
    List,

    /// Show info for a specific custom command
    #[command(about = "Show info for a specific custom command")]
    Info {
        /// Command name
        #[arg(value_name = "NAME")]
        name: String,
    },

    /// Execute a custom command
    #[command(about = "Execute a custom command")]
    Run {
        /// Command name
        #[arg(value_name = "NAME")]
        name: String,

        /// Arguments to pass to the command
        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
        args: Vec<String>,
    },

    /// Load custom commands from a file
    #[command(about = "Load custom commands from a JSON or Markdown file")]
    Load {
        /// Path to command definition file
        #[arg(value_name = "FILE")]
        file: String,
    },

    /// Search for custom commands
    #[command(about = "Search for custom commands by name or description")]
    Search {
        /// Search query
        #[arg(value_name = "QUERY")]
        query: String,
    },
}

#[derive(Subcommand, Debug)]
pub enum ConfigSubcommand {
    /// List all configuration values
    #[command(about = "Display all configuration settings")]
    List,

    /// Get a specific configuration value
    #[command(about = "Get a configuration value by key")]
    Get {
        /// Configuration key (e.g., provider.default, storage.mode)
        #[arg(value_name = "KEY")]
        key: String,
    },

    /// Set a configuration value
    #[command(about = "Set a configuration value")]
    Set {
        /// Configuration key
        #[arg(value_name = "KEY")]
        key: String,

        /// Configuration value
        #[arg(value_name = "VALUE")]
        value: String,
    },
}

/// Route and execute commands
pub struct CommandRouter;

impl CommandRouter {
    /// Parse CLI arguments and route to appropriate handler
    pub fn route() -> CliResult<()> {
        let cli = Cli::parse();

        // Initialize logging based on CLI flags
        crate::logging::init_logging(cli.verbose, cli.quiet);

        Self::execute(&cli)
    }

    /// Execute a command
    pub fn execute(cli: &Cli) -> CliResult<()> {
        match &cli.command {
            Commands::Init { path } => {
                let cmd = InitCommand::new(path.clone());
                cmd.execute()
            }
            Commands::Gen { spec } => {
                let cmd = GenCommand::new(spec.clone());
                cmd.execute()
            }
            Commands::Chat {
                message,
                provider,
                model,
            } => {
                let cmd = ChatCommand::new(message.clone(), provider.clone(), model.clone());
                cmd.execute()
            }
            Commands::Refactor { file } => {
                let cmd = RefactorCommand::new(file.clone());
                cmd.execute()
            }
            Commands::Review { file } => {
                let cmd = ReviewCommand::new(file.clone());
                cmd.execute()
            }
            Commands::Config { action } => {
                let config_action = match action {
                    Some(ConfigSubcommand::List) | None => config::ConfigAction::List,
                    Some(ConfigSubcommand::Get { key }) => config::ConfigAction::Get(key.clone()),
                    Some(ConfigSubcommand::Set { key, value }) => {
                        config::ConfigAction::Set(key.clone(), value.clone())
                    }
                };
                let cmd = ConfigCommand::new(config_action);
                cmd.execute()
            }
            Commands::Completions { shell } => {
                crate::completion::generate_completions(shell).map_err(CliError::Internal)
            }
            Commands::Custom { action } => {
                let custom_action = match action {
                    Some(CustomSubcommand::List) | None => custom::CustomAction::List,
                    Some(CustomSubcommand::Info { name }) => {
                        custom::CustomAction::Info(name.clone())
                    }
                    Some(CustomSubcommand::Run { name, args }) => {
                        custom::CustomAction::Run(name.clone(), args.clone())
                    }
                    Some(CustomSubcommand::Load { file }) => {
                        custom::CustomAction::Load(file.clone())
                    }
                    Some(CustomSubcommand::Search { query }) => {
                        custom::CustomAction::Search(query.clone())
                    }
                };
                let cmd = custom::CustomCommandHandler::new(custom_action);
                cmd.execute()
            }
            Commands::Tui {
                theme,
                vim_mode,
                config,
                provider,
                model,
            } => {
                let config_path = config.as_ref().map(std::path::PathBuf::from);
                let cmd = TuiCommand::new(
                    theme.clone(),
                    *vim_mode,
                    config_path,
                    provider.clone(),
                    model.clone(),
                );
                cmd.execute()
            }
            Commands::Sessions { action } => {
                let sessions_action = match action {
                    Some(SessionsSubcommand::List) | None => sessions::SessionsAction::List,
                    Some(SessionsSubcommand::Create { name }) => {
                        sessions::SessionsAction::Create { name: name.clone() }
                    }
                    Some(SessionsSubcommand::Delete { id }) => {
                        sessions::SessionsAction::Delete { id: id.clone() }
                    }
                    Some(SessionsSubcommand::Rename { id, name }) => {
                        sessions::SessionsAction::Rename {
                            id: id.clone(),
                            name: name.clone(),
                        }
                    }
                    Some(SessionsSubcommand::Switch { id }) => {
                        sessions::SessionsAction::Switch { id: id.clone() }
                    }
                    Some(SessionsSubcommand::Info { id }) => {
                        sessions::SessionsAction::Info { id: id.clone() }
                    }
                    Some(SessionsSubcommand::Share {
                        expires_in,
                        no_history,
                        no_context,
                    }) => sessions::SessionsAction::Share {
                        expires_in: *expires_in,
                        no_history: *no_history,
                        no_context: *no_context,
                    },
                    Some(SessionsSubcommand::ShareList) => sessions::SessionsAction::ShareList,
                    Some(SessionsSubcommand::ShareRevoke { share_id }) => {
                        sessions::SessionsAction::ShareRevoke {
                            share_id: share_id.clone(),
                        }
                    }
                    Some(SessionsSubcommand::ShareInfo { share_id }) => {
                        sessions::SessionsAction::ShareInfo {
                            share_id: share_id.clone(),
                        }
                    }
                    Some(SessionsSubcommand::ShareView { share_id }) => {
                        sessions::SessionsAction::ShareView {
                            share_id: share_id.clone(),
                        }
                    }
                };
                let cmd = SessionsCommand::new(sessions_action);
                cmd.execute()
            }
            Commands::Lsp {
                log_level,
                port,
                debug,
            } => {
                let cmd = lsp::LspCommand::new(log_level.clone(), *port, *debug);
                cmd.execute()
            }
            Commands::Hooks { action } => {
                let hooks_action = match action {
                    Some(HooksSubcommand::List { format }) => hooks::HooksAction::List {
                        format: format.clone(),
                    },
                    None => hooks::HooksAction::List { format: None },
                    Some(HooksSubcommand::Inspect { id, format }) => hooks::HooksAction::Inspect {
                        id: id.clone(),
                        format: format.clone(),
                    },
                    Some(HooksSubcommand::Enable { id }) => {
                        hooks::HooksAction::Enable { id: id.clone() }
                    }
                    Some(HooksSubcommand::Disable { id }) => {
                        hooks::HooksAction::Disable { id: id.clone() }
                    }
                    Some(HooksSubcommand::Delete { id }) => {
                        hooks::HooksAction::Delete { id: id.clone() }
                    }
                };
                let cmd = hooks::HooksCommand::new(hooks_action);
                cmd.execute()
            }
            Commands::Help { topic } => {
                let cmd = HelpCommand::new(topic.clone());
                cmd.execute()
            }
        }
    }

    /// Find similar command for suggestions
    pub fn find_similar(command: &str) -> Option<String> {
        let commands = ["init", "gen", "chat", "refactor", "review", "config", "tui"];

        // Simple similarity check: commands that start with same letter
        commands
            .iter()
            .find(|c| c.starts_with(&command[0..1.min(command.len())]))
            .map(|s| s.to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_find_similar_command() {
        assert_eq!(CommandRouter::find_similar("i"), Some("init".to_string()));
        assert_eq!(CommandRouter::find_similar("g"), Some("gen".to_string()));
        assert_eq!(CommandRouter::find_similar("c"), Some("chat".to_string()));
    }
}