Skip to main content

systemprompt_cli/commands/infrastructure/services/
mod.rs

1//! `infra services` command group: start, stop, restart, status, cleanup, and
2//! serve.
3//!
4//! Defines the [`ServicesCommands`] clap subcommand tree and the individual
5//! target enums ([`StartTarget`], [`StopTarget`], [`RestartTarget`]); dispatch
6//! is delegated to the sibling implementation modules via [`execute`].
7//!
8//! Copyright (c) systemprompt.io — Business Source License 1.1.
9//! See <https://systemprompt.io> for licensing details.
10
11pub mod cleanup;
12mod dispatch;
13mod lifecycle;
14pub mod restart;
15pub mod serve;
16pub mod start;
17mod status;
18mod stop;
19mod types;
20
21use clap::Subcommand;
22use systemprompt_config::ProfileBootstrap;
23
24pub use dispatch::{execute, load_service_configs};
25
26const DEFAULT_API_PORT: u16 = 8080;
27
28pub(super) fn get_api_port() -> u16 {
29    ProfileBootstrap::get().map_or(DEFAULT_API_PORT, |p| p.server.port)
30}
31
32pub(super) fn get_api_addr() -> anyhow::Result<String> {
33    let profile = ProfileBootstrap::get()?;
34    Ok(format!("{}:{}", profile.server.host, profile.server.port))
35}
36
37#[derive(Debug, Clone, Subcommand)]
38pub enum StartTarget {
39    #[command(about = "Start a single agent by name")]
40    Agent { agent: String },
41    #[command(about = "Start a single MCP server by name")]
42    Mcp { server_name: String },
43}
44
45#[derive(Debug, Clone, Subcommand)]
46pub enum StopTarget {
47    #[command(about = "Stop a single agent by name")]
48    Agent {
49        agent: String,
50        #[arg(long, help = "Force stop (SIGKILL)")]
51        force: bool,
52    },
53    #[command(about = "Stop a single MCP server by name")]
54    Mcp {
55        server_name: String,
56        #[arg(long, help = "Force stop (SIGKILL)")]
57        force: bool,
58    },
59}
60
61#[derive(Debug, Subcommand)]
62pub enum ServicesCommands {
63    #[command(
64        about = "Start API, agents, and MCP servers",
65        after_help = "EXAMPLES:\n  systemprompt infra services start\n  systemprompt infra \
66                      services start --api\n  systemprompt infra services start --agents --mcp\n  \
67                      systemprompt infra services start agent <name>"
68    )]
69    Start {
70        #[command(subcommand)]
71        target: Option<StartTarget>,
72
73        #[arg(long, help = "Start all services")]
74        all: bool,
75
76        #[arg(long, help = "Start API server only")]
77        api: bool,
78
79        #[arg(long, help = "Start agents only")]
80        agents: bool,
81
82        #[arg(long, help = "Start MCP servers only")]
83        mcp: bool,
84
85        #[arg(long, help = "Run in foreground (default)")]
86        foreground: bool,
87
88        #[arg(long, help = "Skip database migrations")]
89        skip_migrate: bool,
90
91        #[arg(long, help = "Kill process using the port if occupied")]
92        kill_port_process: bool,
93    },
94
95    #[command(
96        about = "Stop running services gracefully",
97        after_help = "EXAMPLES:\n  systemprompt infra services stop\n  systemprompt infra \
98                      services stop --api\n  systemprompt infra services stop agent <name> \
99                      [--force]"
100    )]
101    Stop {
102        #[command(subcommand)]
103        target: Option<StopTarget>,
104
105        #[arg(long, help = "Stop all services")]
106        all: bool,
107
108        #[arg(long, help = "Stop API server only")]
109        api: bool,
110
111        #[arg(long, help = "Stop agents only")]
112        agents: bool,
113
114        #[arg(long, help = "Stop MCP servers only")]
115        mcp: bool,
116
117        #[arg(long, help = "Force stop (SIGKILL)")]
118        force: bool,
119    },
120
121    #[command(about = "Restart services")]
122    Restart {
123        #[command(subcommand)]
124        target: Option<RestartTarget>,
125
126        #[arg(long, help = "Restart only failed services")]
127        failed: bool,
128
129        #[arg(long, help = "Restart all agents")]
130        agents: bool,
131
132        #[arg(long, help = "Restart all MCP servers")]
133        mcp: bool,
134    },
135
136    #[command(about = "Show detailed service status")]
137    Status {
138        #[arg(long, help = "Show detailed information")]
139        detailed: bool,
140
141        #[arg(long, help = "Output as JSON")]
142        json: bool,
143
144        #[arg(long, help = "Include health check results")]
145        health: bool,
146    },
147
148    #[command(about = "Clean up orphaned processes and stale entries")]
149    Cleanup {
150        #[arg(short = 'y', long, help = "Skip confirmation prompt")]
151        yes: bool,
152
153        #[arg(long, help = "Preview cleanup without executing")]
154        dry_run: bool,
155    },
156
157    #[command(about = "Start API server (automatically starts agents and MCP servers)")]
158    Serve {
159        #[arg(long, help = "Run in foreground mode")]
160        foreground: bool,
161
162        #[arg(long, help = "Kill process using the port if occupied")]
163        kill_port_process: bool,
164    },
165}
166
167#[derive(Debug, Clone, Subcommand)]
168pub enum RestartTarget {
169    #[command(about = "Restart the API server")]
170    Api,
171    #[command(about = "Restart a single agent by name")]
172    Agent { agent: String },
173    #[command(about = "Restart a single MCP server by name")]
174    Mcp {
175        server_name: String,
176        #[arg(long, help = "Rebuild the binary before restarting")]
177        build: bool,
178    },
179}