Back to Blog

Building HTO: A Rust-Powered AI CLI Tool for Developers

November 20, 2024
6 min read

As developers, we spend countless hours in the terminal. Whether it's writing commit messages, creating PR descriptions, or explaining complex commands, these tasks can interrupt our flow. That's why I built HTO - a Rust-powered CLI tool that brings AI assistance directly into your terminal workflow.

The Problem

During my daily development work, I found myself constantly switching contexts:

  • Writing meaningful commit messages after completing features
  • Creating detailed PR descriptions for code reviews
  • Googling terminal commands and breaking my flow
  • Explaining complex code to team members
  • Summarizing files and emails

Each context switch costs mental energy and breaks the development flow. I wanted a tool that could handle these tasks without leaving the terminal.

Why Rust?

I chose Rust for HTO for several key reasons:

Performance

Rust's zero-cost abstractions and memory safety guarantees mean HTO starts up instantly and runs efficiently, even when processing large codebases.

// Example: Fast file processing with Rust
use std::fs;
use std::path::Path;

pub fn analyze_project_structure(path: &Path) -> Result<ProjectAnalysis, Box<dyn std::error::Error>> {
    let mut files = Vec::new();
    
    for entry in fs::read_dir(path)? {
        let entry = entry?;
        let path = entry.path();
        
        if path.is_file() {
            files.push(analyze_file(&path)?);
        }
    }
    
    Ok(ProjectAnalysis { files })
}

Memory Safety

No garbage collection pauses or memory leaks - crucial for a tool that needs to be reliable and fast.

Cross-Platform

Single binary deployment across macOS, Linux, and Windows without runtime dependencies.

Core Features

1. Intelligent Commit Messages

HTO analyzes your git diff and generates meaningful commit messages:

# Instead of writing commit messages manually
hto commit

# Output: "feat: implement user authentication with JWT tokens
# 
# - Add login/logout endpoints
# - Implement JWT token validation middleware  
# - Add user session management
# - Update API documentation"

The tool understands conventional commit formats and analyzes code changes to create descriptive messages.

2. Automated PR Descriptions

Creating comprehensive PR descriptions becomes effortless:

hto pr-description

# Generates structured PR descriptions with:
# - Summary of changes
# - Technical details
# - Testing instructions
# - Breaking changes (if any)

3. Code Review Assistant

Get AI-powered code review suggestions:

hto review src/auth.rs

# Output:
# "Suggestions for src/auth.rs:
# 1. Consider adding input validation for email format
# 2. The password hashing could benefit from constant-time comparison
# 3. Add error handling for database connection failures"

4. Command Explanation

Never google terminal commands again:

hto explain "find . -name '*.rs' -exec grep -l 'async fn' {} \;"

# Output: "This command finds all Rust files (.rs) in the current 
# directory and subdirectories, then searches for files containing 
# 'async fn' and prints their file paths."

Architecture Deep Dive

Configuration-Driven Design

HTO uses a flexible YAML configuration system:

# config.yaml
ai_apps:
  commit_generator:
    prompt: "Generate a conventional commit message based on the git diff"
    model: "gpt-4"
    max_tokens: 200
    
  code_reviewer:
    prompt: "Review this code for potential improvements"
    model: "claude-3"
    context_files: ["*.rs", "*.toml"]
    
  email_summarizer:
    prompt: "Summarize and prioritize these emails"
    model: "gpt-3.5-turbo"

This allows users to create custom AI workflows for their specific needs.

Modular Plugin System

// Plugin trait for extensibility
pub trait HtoPlugin {
    fn name(&self) -> &str;
    fn execute(&self, context: &ExecutionContext) -> Result<String, PluginError>;
    fn supports_file_type(&self, extension: &str) -> bool;
}

// Git plugin implementation
pub struct GitPlugin;

impl HtoPlugin for GitPlugin {
    fn name(&self) -> &str { "git" }
    
    fn execute(&self, context: &ExecutionContext) -> Result<String, PluginError> {
        match context.command.as_str() {
            "commit" => generate_commit_message(),
            "pr-desc" => generate_pr_description(),
            _ => Err(PluginError::UnsupportedCommand)
        }
    }
}

AI Model Integration

HTO supports multiple AI providers through a unified interface:

pub trait AiProvider {
    async fn complete(&self, prompt: &str, options: &CompletionOptions) -> Result<String, AiError>;
}

// OpenAI implementation
pub struct OpenAiProvider {
    client: OpenAiClient,
    api_key: String,
}

impl AiProvider for OpenAiProvider {
    async fn complete(&self, prompt: &str, options: &CompletionOptions) -> Result<String, AiError> {
        let request = CompletionRequest {
            model: options.model.clone(),
            prompt: prompt.to_string(),
            max_tokens: options.max_tokens,
            temperature: options.temperature,
        };
        
        self.client.complete(request).await
    }
}

Performance Optimizations

Async I/O

HTO uses Tokio for async operations, allowing concurrent processing of multiple files:

use tokio::fs;
use futures::stream::{self, StreamExt};

async fn analyze_files_concurrently(file_paths: Vec<PathBuf>) -> Vec<FileAnalysis> {
    stream::iter(file_paths)
        .map(|path| analyze_file_async(path))
        .buffer_unordered(10) // Process 10 files concurrently
        .collect()
        .await
}

Caching Strategy

Frequently accessed data is cached to improve response times:

use std::collections::HashMap;
use std::time::{Duration, Instant};

pub struct ResponseCache {
    cache: HashMap<String, (String, Instant)>,
    ttl: Duration,
}

impl ResponseCache {
    pub fn get(&mut self, key: &str) -> Option<&str> {
        if let Some((value, timestamp)) = self.cache.get(key) {
            if timestamp.elapsed() < self.ttl {
                return Some(value);
            }
            self.cache.remove(key);
        }
        None
    }
}

Real-World Impact

Since releasing HTO, I've seen significant improvements in my development workflow:

  • 50% reduction in time spent writing commit messages
  • More consistent code review quality
  • Fewer context switches when working on complex tasks
  • Better documentation through automated PR descriptions

Open Source and Community

HTO is open source and available on GitHub. The community has contributed:

  • Additional AI provider integrations
  • Custom plugin templates
  • Performance optimizations
  • Documentation improvements

Future Plans

Upcoming features include:

  1. IDE Integration: VS Code extension for seamless integration
  2. Team Workflows: Shared configuration templates
  3. Analytics Dashboard: Track productivity improvements
  4. More AI Providers: Support for local models like Ollama

Getting Started

Install HTO directly:

cargo install hto
# or
brew install hto

Configure your AI provider:

hto config set openai-api-key YOUR_API_KEY

Start boosting your productivity:

hto commit
hto explain "docker run -it --rm ubuntu:latest"
hto review src/main.rs

Conclusion

Building HTO taught me the power of Rust for creating fast, reliable CLI tools. More importantly, it showed how AI can enhance developer productivity when thoughtfully integrated into existing workflows.

The goal isn't to replace developers but to handle the repetitive tasks that interrupt our flow, allowing us to focus on what we do best: solving complex problems and building great software.


Interested in trying HTO? Check out the GitHub repository or share your experience if you're already using it!