os-aware shell command generation with interactive execution
Generates platform-specific shell commands by detecting the user's OS and active shell ($SHELL environment variable), then presents an interactive prompt allowing execution, description, or abortion of the generated command. The DefaultHandler routes the --shell flag to a SHELL SystemRole that constrains LLM output to executable commands. After generation, sgpt parses the response and offers [E]xecute, [D]escribe, [A]bort options, with --no-interaction flag enabling pipeline-friendly non-interactive mode that writes directly to stdout.
Unique: Detects OS and shell environment at runtime to generate platform-specific commands, then wraps generation with an interactive execution gate ([E]xecute/[D]escribe/[A]bort) that prevents blind execution while maintaining pipeline compatibility via --no-interaction flag. This three-way decision point is built into the Handler base class, not a post-processing step.
vs alternatives: Faster context-switching than web search and safer than piping LLM output directly to shell because the interactive prompt forces review before execution, unlike tools that auto-execute or require manual copy-paste.
role-based prompt templating with system context injection
Implements a SystemRole abstraction (defined in sgpt/role.py) that wraps user prompts with role-specific system instructions before sending to the LLM. Built-in roles include SHELL (command generation), DESCRIBE_SHELL (command explanation), CODE (code generation), and GENERAL (Q&A). Roles are selected via CLI flags (--shell, --describe-shell, --code) and mapped through DefaultRoles.check_get() in app.py. Custom roles can be created and persisted via --create-role, allowing users to define domain-specific prompt templates that are reused across sessions.
Unique: Roles are first-class abstractions in the architecture (sgpt/role.py) that decouple prompt templates from CLI logic. The DefaultRoles.check_get() function maps flag combinations to roles, and custom roles are persisted as configuration files, enabling non-developers to create and share role definitions without code changes.
vs alternatives: More flexible than hardcoded prompt prefixes because roles are user-definable and persistent, but less powerful than full prompt engineering frameworks because there's no role composition, versioning, or A/B testing infrastructure.
editor-based prompt input with multi-line support
Allows users to compose prompts in their preferred text editor via the --editor flag, which opens $EDITOR (or a configured editor) for prompt composition. This is useful for long, complex prompts that are cumbersome to type on the command line. The editor integration is implemented in sgpt/utils.py and captures the editor's output as the prompt text. After the user saves and closes the editor, the prompt is sent to the LLM. This enables multi-line prompts, code snippets, and formatted text without shell escaping.
Unique: Editor integration is implemented in sgpt/utils.py as a utility function that launches $EDITOR, captures its output, and returns the text as the prompt. The --editor flag is a simple boolean that triggers this flow in app.py. This allows users to compose prompts in their preferred editor without leaving the terminal.
vs alternatives: More flexible than command-line argument prompts because it supports multi-line input and editor features, but slower because it requires launching an external process. Similar to 'git commit --editor' in workflow but specific to prompt composition.
configuration management with file-based settings
Manages tool configuration via ~/.config/shell_gpt/.sgptrc, a file-based configuration store that persists settings across invocations. Configuration includes API keys, backend selection (USE_LITELLM), model choice, cache TTL, and custom roles. The config.py module handles reading and writing configuration, with sensible defaults for unset values. On first run, sgpt prompts the user for an OpenAI API key and writes it to .sgptrc. Configuration can also be overridden via environment variables (e.g., OPENAI_API_KEY, API_BASE_URL), allowing both file-based and environment-based configuration.
Unique: Configuration is file-based (~/.config/shell_gpt/.sgptrc) and read by config.py at startup, with environment variable overrides for CI/CD flexibility. On first run, sgpt interactively prompts for an API key and writes it to the config file. This hybrid approach supports both interactive setup and automated deployment.
vs alternatives: Simpler than complex configuration systems (YAML, TOML, environment-based) because it uses a flat file format, but less secure because API keys are stored in plaintext. More portable than environment-only configuration because settings persist across sessions.
multi-backend llm routing with provider abstraction
Abstracts LLM provider selection through a Handler base class (sgpt/handlers/handler.py) that supports OpenAI (default), Azure OpenAI, and OpenAI-compatible servers (Ollama, local models) via LiteLLM. Backend selection is controlled by the USE_LITELLM config flag in ~/.config/shell_gpt/.sgptrc and environment variables (API_BASE_URL, OPENAI_API_KEY). The Handler class owns client initialization, request routing, and response streaming, allowing providers to be swapped without changing CLI or role logic. LiteLLM is an optional dependency; if not installed, the tool falls back to OpenAI's official client.
Unique: Handler base class abstracts provider selection at the architecture level, not as a post-hoc wrapper. Backend logic lives in sgpt/handlers/handler.py and is controlled by a single USE_LITELLM config flag; switching providers requires only environment variable changes, not code modifications. LiteLLM is optional, allowing lightweight deployments with OpenAI while supporting advanced users who need local models.
vs alternatives: More flexible than tools locked to a single provider (e.g., GitHub Copilot → OpenAI only) because it supports Ollama and Azure, but less integrated than provider-native SDKs because abstraction adds latency and loses provider-specific optimizations.
persistent chat sessions with conversation history
Maintains multi-turn conversations using the --chat <id> flag, which routes requests to ChatHandler instead of DefaultHandler. Chat sessions are persisted to disk (location managed by sgpt/cache.py) with full conversation history, allowing users to reference previous messages and build context across multiple invocations. Each session is identified by a unique ID; the same ID can be reused to continue a conversation. Session state includes all prior user prompts and LLM responses, enabling the LLM to maintain context without re-sending the entire history on each request (handled by the Handler's context management).
Unique: ChatHandler (separate from DefaultHandler) manages session state by persisting full conversation history to disk and passing it to the LLM on each request. Session IDs are arbitrary user-provided strings, not auto-generated UUIDs, allowing users to name conversations semantically. History is stored in ~/.config/shell_gpt/ alongside configuration, making it portable and inspectable.
vs alternatives: Simpler than full chat applications (no UI, no cloud sync) but more persistent than stateless tools because history survives terminal restarts and can be manually reviewed. Weaker than ChatGPT web UI because there's no conversation search, branching, or multi-device sync.
interactive repl mode with stateful command loops
Provides a read-eval-print loop (REPL) via the --repl <id> flag, which routes requests to ReplHandler and creates an interactive shell-like environment where users can issue multiple prompts in sequence without restarting the tool. Each REPL session maintains state (conversation history, role context) across multiple user inputs, similar to chat sessions but with a continuous interactive loop. The REPL mode is useful for exploratory tasks where users want rapid iteration without the overhead of invoking sgpt multiple times.
Unique: ReplHandler implements a continuous event loop that maintains session state across multiple user inputs, similar to Python's REPL or a shell. Unlike --chat, REPL mode is designed for rapid iteration within a single terminal session and does not persist history by default. The REPL loop is implemented in sgpt/handlers/ and integrates with the same role and caching systems as other handlers.
vs alternatives: More interactive than --chat (no need to re-invoke sgpt for each prompt) but less persistent because history is not saved by default. Similar to ChatGPT's web interface in feel but without the GUI or cloud persistence.
response caching with configurable ttl
Caches LLM responses to disk using sgpt/cache.py, reducing API calls and latency for repeated or similar prompts. Caching is enabled by default (--cache flag) and uses a hash of the prompt, role, and other parameters as the cache key. Cached responses are stored in ~/.config/shell_gpt/ with configurable time-to-live (TTL); expired cache entries are automatically invalidated. The cache is transparent to users — if a cached response exists, it is returned without making an API call. Cache behavior can be controlled via configuration flags.
Unique: Caching is implemented at the Handler base class level (sgpt/cache.py), making it transparent and consistent across all handler types (DefaultHandler, ChatHandler, ReplHandler). Cache keys are deterministic hashes of prompt + role + parameters, and TTL is configurable. Caching is enabled by default but can be disabled per-request or globally via configuration.
vs alternatives: Simpler than distributed caching systems (Redis, Memcached) because it's local and requires no setup, but less powerful because there's no cache invalidation, sharing, or analytics. Faster than making repeated API calls but slower than in-memory caches because responses are read from disk.
+4 more capabilities